繁体   English   中英

有人可以帮我C吗

[英]Can someone help me with C

用C语言编写一个程序,该程序:声明一个最大为80的字符串变量,称为buffer,从键盘上输入一个字符串到缓冲区中,通过替换任何元音(大写或小写字母“ a”,“ e”,“带加号(+)的i','o'或'u')打印出修改后的字符串,并打印出被替换的元音总数

我这样做:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
    char buffer[80];
    char word[80];
    char word2[80];
    scanf("%s",word);
    strcpy(buffer, word);

   int i;
   int counter=0;
   for(i=0;i<80;i++)
   {
      word2[i]="";
   }

   for(i=0;i<strlen(buffer);i++)
   {
   if(buffer[i]=="a" || buffer[i]=="e" || buffer[i]=="i" || buffer[i]=="o" || buffer[i]=="u" || buffer[i]=="A" || buffer[i]=="E" || buffer[i]=="I" || buffer[i]=="O" || buffer[i]=="U")
  //   if(strcmp(buffer[i],"a")==0)
      {
          strcat(word2,"+");
          counter++;
      }else{
       strcat(word2,buffer[i]);
      }
   }

   printf("The modified string is %s",word2);
   printf("in total there was %d vowels.",counter);

   return 0;
}

但是我一直遇到错误,请帮助我。

您需要对字符使用单引号:

buffer[i]=='a'

"a"表示指向字符串文字的指针,该字符串文字将存储在程序地址空间的某些段中。

尝试这个

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char buffer[80];
    char word[80];
    char word2[80];
    scanf("%s",word);
    strcpy(buffer, word);

    int i;
    int counter=0;


    for(i=0;i<strlen(buffer);i++)
    {
        if(buffer[i]=='a'|| buffer[i]=='e' || buffer[i]=='i' || buffer[i]=='o' || buffer[i]=='u' || buffer[i]=='A' || buffer[i]=='E' || buffer[i]=='I' || buffer[i]=='O' || buffer[i]=='U')
        {
           word[i] = '+';
           counter++;
        }

    }

    printf("The modified string is %s",word);

    printf("in total there was %d vowels.",counter);

    return 0;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM