C中的复数程序,当我运行程序时,复数单词后面有一堆随机的东西

时间:2017-11-21 01:45:28

标签: c

所以问题在于,无论何时我输入一个单词,该单词的复数形式都会有一堆跟随它的随机垃圾。这是一个 example of the problem 。这是大部分代码(如果您需要更多代码,我会提供更多代码),如果您发现任何其他问题,请告诉我们? 非常感谢你。

char * getWord()
{
    //make char array of size 20 using malloc
    char *word;
    word = malloc(20);
    //give the msg to user
    printf("Enter a word ");
    //take input from user
    scanf("%s", word);
    printf("\n");
    //return word
    return word;
}
//change word to uppercase
void toUpperCase(char *word)
{
    int i = 0;
    while (word[i] != '\0')
    {
        if (word[i] >= 65 && word[i] <= 90)
        {
            //it is upper case latter so do nothing
        }
        else if (word[i] >= 97 && word[i] <= 122)
        {
            word[i] = word[i] - 32;
        }
        i++;
    }
}
//function which will determine which rule applies to given word
int findRule(char *word)
{
    int n;//length of word
    n = strlen(word);

    //last char is Y then rule 1
    if (word[n - 1] == 'Y')
        return 1;
    //end is S,CH,SH then rule 2
    else if ((word[n - 1] == 'S') || (n >= 2 && (word[n - 1] == 'H' && (word[n - 2] == 'C' || word[n - 2] == 'S'))))
    {
        return 2;
    }
    //else rule 3
    else return 3;
}
//rule one
char *ruleOne(char *word)
{
    int n;//length of word
    n = strlen(word);
    //make char array of size 20 using malloc
    char *ans;
    ans = malloc(n + 2);
    //copy all char except last
    for (int i = 0; i<n - 1; i++)
    {
        ans[i] = word[i];
    }
    //add ies in place of y
    ans[n - 1] = 'I';
    ans[n] = 'E';
    ans[n + 1] = 'S';
    return ans;
}

//rule 2
char *ruleTwo(char *word)
{

    int n;//length of word
    n = strlen(word);
    //make char array of size 20 using malloc
    char *ans;
    ans = malloc(n + 2);
    //copy all char
    for (int i = 0; i<n; i++)
    {
        ans[i] = word[i];
    }
    //add two char E and S
    ans[n] = 'E';
    ans[n + 1] = 'S';
    return ans;
}
//rule 3
char *ruleThree(char *word)
{
    int n;//length of word
    n = strlen(word);
    //make char array of size 20 using malloc
    char *ans;
    ans = malloc(n + 1);
    //copy all char
    for (int i = 0; i<n; i++)
    {
        ans[i] = word[i];
    }
    //add S
    ans[n] = 'S';
    return ans;
}

int main()
{
    //geets the user
    greets();
    //open file to write

    fptr = fopen("pluralWords.txt", "w");
    //if file not open
    if (fptr == NULL)
    {
        printf("Error to open file!\n");
        exit(1);
    }
    else
    {
        //file opened
        printf("%s\n", "The output file pluralWords.txt is open\n");
        printf("%s\n", "---------------------------------------\n");
        while (1)
        {

            //ask the msg that user want enter a number or not y/Y(YES), n/N (NO)
            printf("%s", "Would you like to enter a word Y (YES) or N (NO)?");
            //it is for check yes or no
            char c[2];
            scanf("%s", c);

            if (c[0] == 'n' || c[0] == 'N')
            {
                printf("Thank you for trying out the Pluralizer!");
                printf("\nClosing the file pointer");
                fclose(fptr);
                break;
            }
            else if (c[0] == 'y' || c[0] == 'Y') {
                printf("\n");
                //make cahr array using pointer and malloc function
                char *word = malloc(20);
                //read input
                word = getWord();
                //convert to upper case
                toUpperCase(word);
                printf("%s\n", word);

                //find rule
                int rule = findRule(word);

                printf("Rule is %d\n", rule);

                char *ans = malloc(22);
                //call coresponding function with rule
                if (rule == 1)
                {

                    //call ruleOne
                    ans = ruleOne(word);

                }
                else if (rule == 2)
                {

                    //call ruleTwo
                    ans = ruleTwo(word);
                }
                else
                {

                    //call ruleThree
                    ans = ruleThree(word);

                }
                //output to screen
                printf("Word is %s and plural is %s\n\n", word, ans);
                //msg to user
                printf("Adding the words to the file\n");
                //wrtie to file
                fprintf(fptr, "%s   %s\n", word, ans);
                printf("\n\n");

1 个答案:

答案 0 :(得分:0)

C字符串最后需要一个空字符,即:Error: doc.find is not a function ,其中'\ 0'为空字符。

这个空字符是操作知道字符串长度的方式。诸如['s', 't', 'r', 'i', 'n', 'g', '\0']strlen之类的字符串函数会查找此空字符,并会一直查看内存,直到找到它为止。在打印出来的情况下,这意味着程序将打印出“垃圾”内存,直到找到设置为\ 0的位置。

在你的规则...函数中,你覆盖这个空字符而不是替换它:

strcpy

顺便说一下,你也是malloc'ing内存但从来没有释放它会导致内存泄漏。在C中,将缓冲区传递给函数通常更简洁,而不是让它具有malloc内存并返回它(例如ans[n] = 'E'; //this was NULL (/0) ans[n + 1] = 'S'; return ans; //ans is now missing a NULL terminator 会更好char* ruleOne(char* word))然后您可以在main()中创建一个缓冲区作为void ruleOne(char* in_word, char* out_word, int out_len)而不必担心自己管理记忆。