【问题标题】:I am doing an assignment that asks me to make a function that creates an acronym from a string, then return the acronym我正在做一个任务,要求我创建一个从字符串创建首字母缩写词的函数,然后返回首字母缩写词
【发布时间】:2021-07-14 23:29:18
【问题描述】:

我得到的提示是:首字母缩略词是由一组短语中单词的首字母组成的单词。编写一个程序,其输入是一个短语,其输出是输入的首字母缩写词。如果单词以小写字母开头,请不要在首字母缩写词中包含该字母。假设输入中至少有一个大写字母。

另外,我使用了以下函数:void CreateAcronym(char userPhrase[], char userAcronym[])。

我的代码问题是只有第一个字母被保存到 userAcronym 变量中。 例如,当字符串是电气和电子工程师协会时。我得到的输出只是 I。我需要更改什么才能获得剩余的字母?

感谢您的帮助。

到目前为止我的代码是:

#include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #define MAX 60

     void CreateAcronym(char userPhrase[], char userAcronym[]){
       int i;
   
   
       int j=0;
       for(i = 0; i < strlen(userPhrase); ++i){
       if(isupper(userPhrase[i])){
     
       userAcronym[j]=userPhrase[i];
       }
       j++;
       }
       printf("%s", userAcronym);
     }

     int main(void) {
     char phrase[MAX];
     char acronym[10];
   
     fgets(phrase, MAX, stdin);
     CreateAcronym(phrase, acronym);
   

     return 0;
     }

【问题讨论】:

  • 一个问题是输入数组的长度为 60 个字符,而输出数组的长度为 10 个字符。那只是乞求缓冲区溢出。
  • 你需要增加j inside isupper 块。
  • 并在末尾添加一个空字符。
  • @EmanuelP 您的意思是执行以下操作吗? if(isupper(userPhrase[i])){ userAcronym[j]=userPhrase[i]; j++; printf("\0"); }
  • @Ale 差不多。当循环结束时,你应该做userAcronym[j] = '\0' 这将使它成为一个以零结尾的字符串。 printf 是在正确的位置。应该就在那之前。

标签: c substring c-strings uppercase function-definition


【解决方案1】:

对于初学者来说,函数CreateAcronym 至少应该声明为

void CreateAcronym( const char userPhrase[], char userAcronym[]);

因为传递的包含短语的字符串在函数内没有被更改。

但最好把函数声明成这样

char * CreateAcronym( const char userPhrase[], char userAcronym[]);

该函数不应输出任何内容。函数的调用者决定是否输出函数内部形成的首字母缩写词。

该函数调用未定义的行为,因为数组首字母缩写词没有得到字符串。

此外,for循环中还有另一个错误

for(i = 0; i < strlen(userPhrase); ++i){
   if(isupper(userPhrase[i])){
       userAcronym[j]=userPhrase[i];
   }
   j++;

}

变量j在循环的每次迭代中递增。

并且在循环的条件下调用函数strlen是低效的。

此外,该函数不会仅将单词的首字母大写字母复制到目标数组。它试图复制任何大写字母。所以无论如何for循环都没有意义。

该函数可以通过以下方式定义,如下面的演示程序所示。

#include <stdio.h>
#include <ctype.h>

char *  CreateAcronym( const char userPhrase[], char userAcronym[] )
{
    char *p = userAcronym;
    
    while ( *userPhrase )
    {
        while ( isspace( ( unsigned char )*userPhrase ) ) ++userPhrase;
        
        if ( isupper( ( unsigned char )*userPhrase ) ) *p++ = *userPhrase;
        
        while ( *userPhrase && !isspace( ( unsigned char )*userPhrase ) ) ++userPhrase;
    }

    *p = '\0';
    
    return userAcronym;
}

int main(void) 
{
    const char *phrase = "Institute of Electrical and Electronics Engineers";
    char acronym[10];
    
    puts( CreateAcronym( phrase, acronym ) );
    
    return 0;
}

程序输出是

IEEE

【讨论】:

    【解决方案2】:

    试试看。首先,您在“if”中使用了 j++。其次,您没有在 userAcronym 字符串中输入 '\0'。 '\0' 表示你的字符串在这里结束,所有字符串都将打印在这个符号之前。

        #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #define MAX 60
    
    void CreateAcronym(char userPhrase[], char userAcronym[]){
        int i;
        int j = 0;
        for(i = 0; i < strlen(userPhrase); i++){
            if(isupper(userPhrase[i])){
                userAcronym[j] = userPhrase[i];
                j++;
            }
        }
        userAcronym[j] = '\0';
        printf("%s", userAcronym);
    }
    
    int main(){
        char phrase[MAX];
        char acronym[10];
        fgets(phrase, MAX, stdin);
        CreateAcronym(phrase, acronym);
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 2019-08-14
      • 1970-01-01
      • 2013-12-17
      • 1970-01-01
      相关资源
      最近更新 更多