C语言 统计字符出现的次数

来源:百度知道 编辑:UC知道 时间:2024/05/28 06:14:46
统计各个数字、空白符(包括空格符、制表符及换行符)以及所有其他字符出现的次数。
输入:输入测试数据直到文件结束。
输出:输出有三行,分别对应各个数字(0-9)出现的次数(数据之间有一个空格),空白符出现的次数,所有其他字符出现的次数。
输入样例:$%^ 87we
输出样例:
0 0 0 0 0 0 0 1 1 0
2
5
输入样列中,最后有一个换行符,所以其它字符总共有5个。

#include <stdio.h>

int main(void)
{
int num[10]={0};
int space=0,sels=0;
char ch;

while((ch=getchar())!=EOF)
{
if(ch>='0'&&ch<='9')
num[ch-'0']++;
else if(ch==' '||ch=='\t'||ch=='\n')
space++;
else
sels++;
}

for(ch=0;ch<10;ch++)
printf("%d ",num[ch]);
putchar('\n');

printf("%d\n",space);
printf("%d\n",sels);

return 0;
}

#include<stdio.h>
void main()
{ int num[10]={0},k=0,ech=0;
char str[100]="this is a test.27 276 \t \n ";
char *s=str;
while(*s)
{ if(*s>=48 && *s<=57) num[*s-48]++;
else if(*s==' ' || *s=='\t' || *s=='\n'