您的位置:首页 > 编程语言 > C语言/C++

统计字符

2015-11-24 00:35 253 查看
写一个程序统计输入字符串中:
各个数字、空白字符、以及其他所有字符出现的次数。
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>//isspace(),isdigit()

int main()
{
int space = 0;
int other = 0;
int arr[10] = { 0 };
int ch = 0;
int i = 0;

while ((ch = getchar()) != EOF)
{
if (isspace(ch))
{
space++;
}
else if (isdigit(ch))
{
arr[ch - '0']++;
}
else
other++;
}

printf("space:%d\n", space);
printf("other:%d\n", other);

for (i = 0; i < 10; i++)
{
printf("%d:%d\n", i, arr[i]);
}
system("pause");

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C语言 字符串