c语言字符统计
输入若干行字符,统计其字符数、行数和空白符(空格、回车、制表符)的个数
#include<stdio.h>
main()
{
char chr[128]={0};
char str[256];
int i, rows=0;
while(1)
{
gets(str);
if (*str == 0)
break;
rows++;
for (i=0; str[i]; i++)
chr[str[i]]++;
}
printf("行数 %d\n", rows);
printf("字符\t个数\n");
for (i=0; i<128; i++)
{
if (chr[i] == 0)
continue;
if (i == 9)
printf("%s\t%d\n", "\\t", chr[i]);
else if (i == 10)
printf("%s\t%d\n", "\\n", chr[i]);
else if (i == 13)
printf("%s\t%d\n", "\\r", chr[i]);
else if (i == 32)
printf("%s\t%d\n", "' '", chr[i]);
else
printf("%c\t%d\n", i, chr[i]);
}
}