[英]Can't get a function call in a function to work properly
I'm writing a program to generate a string of random uppercase letters, then take user input of uppercase letters, along with a character form the user. 我正在编写一个程序,以生成一串随机的大写字母,然后接受用户输入的大写字母以及用户使用的字符。 For any instance of the user input letter in the random string, it replaces that letter with the character entered by the user.
对于随机字符串中用户输入字母的任何实例,它将用用户输入的字符替换该字母。
For example, s1 = {BDHFKYL} s2 = {YEIGH} c = '*' 例如,s1 = {BDHFKYL} s2 = {YEIGH} c ='*'
Output = BD*FK*L 输出= BD * FK * L
The program was working correctly until I added the feature to ask the user to enter what character they would like to replace the letters. 该程序正常运行,直到我添加了功能要求用户输入要替换字母的字符。
The output is: 输出为:
Please enter at least 2 capital letters and a maximum of 20.
HDJSHDSHDDS
HDJSHDSHDDS
Enter a character to replace occuring letters.
*
NWLRBBMQB
Would you like to enter another string?
Here's the code: 这是代码:
void fillS1(char x[]);
void fillS2(char x[], char y[], char z);
void strFilter(char a[], char b[], char c);
int main(int argc, const char * argv[])
{
char s1[42];
char s2[22];
char x = 0;
fillS2(s2, s1, x);
return 0;
}
void fillS1(char x[])
{
for (int i = 0; i < 40; i++)
x[i] = 'A' + random() % 26;
x[40] = (char)0;
}
void fillS2(char x[], char y[], char z){
char loopContinue = 0;
do {
int i = 0;
int capitalLetterCheck = 0;
printf("Please enter at least 2 capital letters and a maximum of 20.\n");
while (( x[i] = getchar()) != '\n' ) {
i++;
}
x[i] = '\0';
if (i < 3) {
printf("You need at least two letters\n");
}
else if (i > 21){
printf("You cannot have more than twenty letters\n");
}
for (i = 0; i < 20; i++) {
if ((x[i] >= 'a') && (x[i] <= 'z')) {
printf("You many only have capital letters.\n");
capitalLetterCheck = 2;
}
}
if (capitalLetterCheck != 2) {
for (i = 0; i < 20; i++) {
if ((x[i] >= 'A') && (x[i] <= 'Z')) {
puts(x);
fillS1(y);
printf("Enter a character to replace occuring letters.\n");
while ((z = getchar() != '\n')) {
}
strFilter(y, x, z);
break;
}
}
}
printf("Would you like to enter another string?\n");
gets(&loopContinue);
} while (loopContinue != 'n');
}
void strFilter(char a[], char b[], char c){
int i = 0;
int n = 0;
while (n < 20) {
for (i = 0; i < 40; i++) {
if (a[i] == b[n]){
a[i] = c;
}
}
i = 0;
n++;
}
puts(a);
}
Thank you. 谢谢。
first of all please try to make your code a little easier to read, and I'm not talking about indenting but about its flow. 首先,请尝试使您的代码更易于阅读,我不是在谈论缩进,而是在谈论其流程。
Also, your example output seems to work fine since there was nothing to change in any string here...? 另外,您的示例输出似乎可以正常工作,因为这里的任何字符串都没有变化...?
There are a few things you should keep in mind when coding : 编码时应牢记以下几点:
It's very easy to write some pseudo-code and then translate it into C : 编写一些伪代码然后将其转换为C是非常容易的:
WHILE someCondition
Generate a random string
Get a string from the user
Get a character from the user
Find and replace
END
Here is an example of how you could have organised your code (don't use it though - no frees, no getting the user's input, etc.) : 这是一个示例,说明如何组织代码(不过请不要使用它-没有释放,没有得到用户的输入等):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* generateString(void)
{
return "AEIOUYAEIOUY"; // In your implementation, this is where you'd generate the random string
}
char* getStringInput(void)
{
return "HELLO"; // In your implementation, this is where you'd get the user's string
}
char getCharInput(void)
{
return '*'; // In your implementation, this is where you'd get the user's character
}
char* findAndReplace(char* randomString, char* userString, char userChar)
{
int l1;
int l2;
int i;
int j;
char* output;
l1 = strlen(randomString);
l2 = strlen(userString);
output = (char*)malloc(sizeof(*output) * l1);
strcpy(output, randomString);
for (i = 0; i < l1; ++i)
{
for (j = 0; j < l2; ++j)
if (randomString[i] == userString[j])
output[i] = userChar;
}
return (output);
}
int main(int ac, char** av)
{
char* randomString;
char* userString;
char userChar;
char* outputString;
randomString = generateString();
userString = getStringInput();
userChar = getCharInput();
outputString = findAndReplace(randomString, userString, userChar);
printf("Result: %s\n", outputString);
// don't forget to free any allocated buffer
return (1);
}
How much debugging have you done? 您完成了多少调试? Try putting some printfs in your code to see what happens - when functions are called, what are your variable's values, etc. Example :
尝试在代码中放入一些printfs,看看会发生什么-调用函数时,变量的值是多少,等等。例如:
void fillS1(char x[])
{
printf("-- entering fillS1, buffer value: %s\n", x);
for (int i = 0; i < 40; i++)
x[i] = 'A' + random() % 26;
x[40] = (char)0;
printf("-- leaving fillS1, buffer value: %s\n", x);
}
(be careful about what's in your buffer before you use printf) (在使用printf之前,请注意缓冲区中的内容)
This should tell you pretty quickly what's going wrong. 这应该很快告诉您出了什么问题。
For example, try checking the value of "c" in strFilter when it's called, and have a second look at how you get the user's input. 例如,尝试在调用strFilter时检查“ c”的值,然后再看看如何获得用户输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.