0

我正在编写一个程序来生成一串随机大写字母,然后获取用户输入的大写字母以及用户的字符。对于随机字符串中用户输入字母的任何实例,它会将该字母替换为用户输入的字符。

例如,s1 = {BDHFKYL} s2 = {YEIGH} c = '*'

输出 = BD*FK*L

在我添加了要求用户输入他们想要替换字母的字符之前,该程序一直运行正常。

输出是:

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?

这是代码:

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);
}

谢谢你。

4

1 回答 1

4

首先,请尽量让您的代码更易于阅读,我不是在谈论缩进,而是在谈论它的流程。

此外,您的示例输出似乎工作正常,因为这里的任何字符串都没有任何改变......?

编码时应牢记以下几点:

  • 给你的变量和函数明确的名字,特别是如果你打算让某人在某个时候阅读你的代码
  • 当你有一个特定的任务要执行(获取用户的输入,生成一个随机字符串等)时,尝试通过制作小函数来保持你的代码流简单,而不是仅仅将大部分代码写在叠层循环中
  • 您还可以查看 scanf (man scanf) 以获取用户的输入
  • 尝试在获得用户输入时分配缓冲区,而不是使用大小可能不正确的静态缓冲区

编写一些伪代码然后将其翻译成 C 非常容易:

WHILE someCondition
    Generate a random string
    Get a string from the user
    Get a character from the user
    Find and replace
END

这是一个如何组织代码的示例(尽管不要使用它 - 没有释放,没有获取用户的输入等):

#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);
}

你做了多少调试?尝试在您的代码中添加一些 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);
}

(在使用 printf 之前,请注意缓冲区中的内容)

这应该很快告诉你出了什么问题。

例如,尝试在调用 strFilter 时检查“c”的值,然后再看看如何获​​得用户的输入。

于 2012-11-04T08:37:04.880 回答