有人可以用C帮我

时间:2016-03-01 04:23:13

标签: c

用C编写一个程序: 声明一个名为buffer的字符串变量,最大大小为80 从键盘输入一个字符串到缓冲区 通过替换任何元音来修改缓冲区中包含的字符串(大写或小写' 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;
}

但我一直在收到错误,请帮助我。

2 个答案:

答案 0 :(得分:3)

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

buffer[i]=='a'

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

答案 1 :(得分:3)

试试这个

#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;

}