无法在函数中获取函数调用以正常工作

时间:2012-11-04 03:41:15

标签: c function

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

例如,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);
}

谢谢。

1 个答案:

答案 0 :(得分:4)

首先请尝试让您的代码更容易阅读,我不是在讨论缩进,而是讨论它的流程。

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

编码时应注意以下几点:

  • 为您的变量和函数提供明确的名称,特别是如果您希望某人在某个时候阅读您的代码
  • 尝试通过在执行特定任务(获取用户输入,生成随机字符串等)时创建小函数来简化代码流程,而不是仅仅在复杂循环中编写大部分内容< / LI>
  • 您还可以查看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”的值,然后再看看如何获​​得用户的输入。