http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=1§ionid=2&problemid=26
统计输入字符串各字符的数目:
1、gets()的用法:
从stdin流中读取字符串,直至接受到换行符或EOF时停止,并将读取的结果存放在buffer指针所指向的字符数组中。换行符不作为读取串的内容,读取的换行符被转换为‘\0’空字符,并由此来结束字符串。
本函数可以无限读取,不会判断上限,所以程序员应该确保buffer的空间足够大,以便在执行读操作时不发生溢出。
用法:
char str[100];
gets(str);
2、memset()的用法
头文件:<memory.h> or <string.h>
函数介绍
void *memset(void *s, int ch,
size_t n);
注意:不要搞反 ch 与 n.
用法:
memset(a,0,sizeof(a))
#include <stdio.h>
#include <string.h>
int main()
{
char buf[100001];
int count[26];
while(gets(buf))
{
memset(count,0,sizeof(count));
int len = strlen(buf);
for(int i=0;i<len;i++)
{
if(buf[i] >= 'a' && buf[i] <='z')
{
count[buf[i] - 'a']++;
}
}
for(int j=0;j<26;j++)
{
printf("%c:%d\n", (char)('a'+j),count[j]);
}
printf("\n");
}
return 0;
}