实验7-3-3 统计大写辅音字母(字符串数组的输入以及长度的统计编程)

实验7-3-3 统计大写辅音字母
英文辅音字母是除A、E、I、O、U以外的字母。本题要求编写程序,统计给定字符串中大写辅音字母的个数。

输入格式:
输入在一行中给出一个不超过80个字符、并以回车结束的字符串。

输出格式:
输出在一行中给出字符串中大写辅音字母的个数。

输入样例:
HELLO World!

输出样例:
4

#include<stdio.h>
#include<string.h>
int main(){
      char str[80];
      scanf("%s",str);             //这里不需要使用取地址符;
      int i=0;;
      int cnt=0;
      int n=strlen(str);
      printf("%d \n",n);
      char ch=getchar();
      while(ch!='\n'){
                  if(ch>'A'&&ch<='Z'&&ch!='A'&&ch!='E'&&ch!='I'&&
                     ch!='O'&&ch!='U'){
                        cnt++;
                  
                  }
            i++;
            ch=getchar();
      }
      printf("%d\n",cnt);
      return 0;
}

HELLO World!
5 ----->n=strlen(str)=5;这里的统计不对;
1
Program ended with exit code: 0

#include<stdio.h>
int main(){
      char str[80];
      int cnt=0,i=0;
      char ch;
      ch=getchar();
      while(ch!='\n'){
            str[cnt]=ch;
            cnt++;
            ch=getchar();
            printf("%c",str[cnt]);
      }
      str[cnt]='\0';
      int flag=0;
      for(i=0;i<cnt;i++){
            if(str[i]>='A'&&str[i]<='Z'&&str[i]!='A'&&str[i]!='E'
               &&str[i]!='I'&&str[i]!='O'&&str[i]!='U'){
                  flag++;
            }
      }
      printf("%d\n",flag);
      return 0;
}

发布了54 篇原创文章 · 获赞 0 · 访问量 990

猜你喜欢

转载自blog.csdn.net/hellobettershero/article/details/103967299
今日推荐