hdu-2027题&&gets/getchar的区别

时间:2023-03-09 00:51:04
hdu-2027题&&gets/getchar的区别

hdu-2027题(水题~~~)

统计每个元音字母在字符串中出现的次数。
Input输入数据首先包括一个整数n,表示测试实例的个数,然后是n行长度不超过100的字符串。
Output对于每个测试实例输出5行,格式如下:
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
 #include<iostream>
#include<cstring>
using namespace std;
int main()
{
int n,n1,n2,n3,n4,n5;
cin>>n;
getchar();//
while(n--)
{ char str[];
gets(str);
int len=strlen(str);
n1=;n2=;n3=;n4=;n5=;
for(int i=;i<len;i++)
{
if(str[i]=='a')
n1++;
if(str[i]=='e')
n2++;
if(str[i]=='i')
n3++;
if(str[i]=='o')
n4++;
if(str[i]=='u')
n5++;
}
cout<<"a:"<<n1<<endl;
cout<<"e:"<<n2<<endl;
cout<<"i:"<<n3<<endl;
cout<<"o:"<<n4<<endl;
cout<<"u:"<<n5<<endl;
if(n!=)
cout<<endl;
}
}
 

//get()与getchar()的区别

gets()用于从标准输入流stdin读入一个整行(以'\n'或EOF)结束,写入ptr指向的字符数组,并返回这个指针;出错或遇到文件结束时则返回NULL。行末的'\n'从流中取出,但不写入数组。gets()不检查被写入的数组大小。

getchar()用于从标准输入流stdin读入一个字符,并返回这个字符。如果读到文件结尾,则返回EOF。注意到EOF不能用char类型表示,所以getchar()函数返回的是一个int型的数。

//一句话,get读入字符串(含空格--优于getline函数),而getchar读入一个数(int).

Reason:   getline用法:cout.getline(str,100,'#')(c++语言描述).大多输入未知长度,所以用get方便得多。

2017-02-04