C语言编程:从单数名词到复数名词

C语言编程:从单数名词到复数名词,c,C,我这里的程序应该是 编写一个程序,根据规则提取名词并形成复数: 如果名词以“y”结尾,去掉“y”并加上“y” 若名词以“s”、“c”、“ch”或“sh”结尾,则添加“es” 在所有其他情况下,只需添加“s” 打印每个名词及其复数 它工作得很好,当我输入单词,比如dairy,它会打印dairies,但它会循环并打印dairies…等等。我希望这是一个快速修复,我找不到,有人可以帮我 谢谢 #include <stdio.h> #include <string.h> #

我这里的程序应该是

编写一个程序,根据规则提取名词并形成复数:

  • 如果名词以“y”结尾,去掉“y”并加上“y”
  • 若名词以“s”、“c”、“ch”或“sh”结尾,则添加“es”
  • 在所有其他情况下,只需添加“s”
  • 打印每个名词及其复数

    它工作得很好,当我输入单词,比如dairy,它会打印dairies,但它会循环并打印dairies…等等。我希望这是一个快速修复,我找不到,有人可以帮我

    谢谢

    #include <stdio.h>
    #include <string.h>
    
    
    
    #define max_word 20
    
    
    
    /* prototypes */
    void pluralize (char word[]);
    
    
    int main (void)
    { 
      char noun[max_word];   /* stores temporary word entered by user */
    
      printf("Enter a noun in singular form: ");
      scanf("%s", noun);
    
      while (strcmp(noun, "done") != 0)
     {
        pluralize (noun);
        printf("The plural form is %s\n", noun);
     }
    
      return;
    }
    
    void pluralize (char word[])
    {
      int length;
      char noun;
      length=1;
      length = strlen(word);
    
       if (word[length - 1] == 'y') 
     {   word[length - 1] = 'i';
         word[length] = 'e';
        word[length + 1] = 's';
        word[length + 2] = '\0';  
      }
    
    
      /* if word ends in "s" "ch" or "sh" add "es" */
    
     else if (word[length - 1] == 's' ||
        (word[length - 2] == 'c' && word[length - 1] == 'h') ||
        (word[length - 2] == 's' && word[length - 1] == 'h'))
      {  strcat(word, "es");
      }
    
      else
     {   strcat(word, "s");
    
        printf("New word is: ", &noun);
    } 
    return;
    }
    
    #包括
    #包括
    #定义max_word 20
    /*原型*/
    void复数(char-word[]);
    内部主(空)
    { 
    字符名词[max_word];/*存储用户输入的临时单词*/
    printf(“输入单数形式的名词:”);
    scanf(“%s”,名词);
    while(strcmp(名词,“done”)!=0)
    {
    复数化(名词);
    printf(“复数形式为%s\n”,名词);
    }
    返回;
    }
    void复数(字符单词[])
    {
    整数长度;
    字符名词;
    长度=1;
    长度=strlen(字);
    if(字[length-1]=“y”)
    {word[length-1]='i';
    字[长度]=“e”;
    单词[长度+1]=“s”;
    字[长度+2]='\0';
    }
    /*如果单词以“s”、“ch”或“sh”结尾,则添加“es”*/
    如果(单词[长度-1]='s'||
    (单词[length-2]=“c”和单词[length-1]=“h”)||
    (单词[length-2]='s'&&word[length-1]='h'))
    {strcat(单词“es”);
    }
    其他的
    {strcat(单词“s”);
    printf(“新词是:”、&名词);
    } 
    返回;
    }
    
    我假设如果用户输入文本
    done
    ,您希望程序终止。如果是这样,您需要修复循环:

    for (;;) 
    {
        printf("Enter a noun in singular form: ");
        scanf("%s", noun);
    
        if ((strcmp(noun, "done") == 0)
            break;
    
        pluralize (noun);
        printf("The plural form is %s\n", noun);
    }
    
    当前版本将循环,直到
    pluralize()
    将字符串设置为
    done
    ,这将永远不会发生


    作为旁注,您应该使用
    strncmp()
    strncat()
    来避免潜在的缓冲区溢出。这在这类代码中并不太重要,但如果您编写的内容面向不受信任的用户,那么使用
    strcmp()
    strcat()

    可能会使您自己面临严重的安全漏洞。如果您不想让它循环,为什么主代码会有一个while循环?这是一个非常好的问题。好像已经修好了。谢谢,haait不是您要查找的问题,但是
    main
    返回一个
    int
    。确保块末尾的
    return
    语句反映了这一点。如果要在单个会话中对多个单词进行复数,则需要一个循环。你正在检查“完成”的事实表明情况就是这样。我认为你的导师在第二条规则中忘记了“x”…;-)