HDU_oj_2027 统计元音
Problem Description
统计每个元音字母在字符串中出现的次数。
Input
输入数据首先包括一个整数n,表示测试实例的个数,然后是n行长度不超过100的字符串。
Output
对于每个测试实例输出5行,格式如下:
a:num1
e:num2
i:num3
o:num4
u:num5
多个测试实例之间由一个空行隔开。
请特别注意:最后一块输出后面没有空行:)
a:num1
e:num2
i:num3
o:num4
u:num5
多个测试实例之间由一个空行隔开。
请特别注意:最后一块输出后面没有空行:)
Sample Input
2
aeiou
my name is ignatius
Sample Output
a:1
e:1
i:1
o:1
u:1
a:2
e:1
i:3
o:0
u:1
分析:
注意点:
用gets()函数接收字符串,由于gets()函数会接收到前面输入数字n时的回车符,
所以在get()前需要用getchar()函数吸收这个回车符
1 #include<iostream> 2 using namespace std; 3 4 int main() 5 { 6 char *ss,s[100]; 7 int n,na,ne,ni,no,nu; 8 cin>>n; 9 getchar(); 10 while(n--) 11 { 12 gets(s); 13 ss=s; 14 na=ne=ni=no=nu=0; 15 while(*ss!='\0') 16 { 17 if(*ss=='a') 18 na++; 19 else if(*ss=='e') 20 ne++; 21 else if(*ss=='i') 22 ni++; 23 else if(*ss=='o') 24 no++; 25 else if(*ss=='u') 26 nu++; 27 ss++; 28 } 29 cout<<"a:"<<na<<endl; 30 cout<<"e:"<<ne<<endl; 31 cout<<"i:"<<ni<<endl; 32 cout<<"o:"<<no<<endl; 33 cout<<"u:"<<nu<<endl; 34 if(n) 35 cout<<endl; 36 } 37 }