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

C语言统计一串字符中空格键、Tab键、回车键、字母、数字及其他字符的个数(Ctrl+Z终止输入)

2018-03-18 10:37 761 查看

具体代码如下所述:

#include<stdio.h>
void main(){
int c, letter=0, num=0, blank=0, tab=0, enter=0, other=0 ,i=0, sum=0;
printf("Please input a string:\n");
while((c=getchar())!=EOF){
sum++;
if(c==' '){
++blank; //空格键的个数
}
else if(c=='\t'){
++tab;  //Tab键的个数
}
else if(c=='\n'){
++enter;  //回车键的个数
}
else if((c>='A' && c<='Z') || (c>='a' && c<='z')){
++letter;  //字母的个数
}
else if(c>='0' && c<='9'){
++num;   //数字的个数
}
else ++other;   //其他字符的个数
i++;
}
printf("There are %d characters\n", sum);
printf("blank=%d, Tab=%d, Enter=%d, letter=%d, number=%d ,other=%d\n",blank, tab, enter, letter, num, other);
}

结果为:

总结

以上所述是小编给大家介绍的C语言统计一串字符中空格键、Tab键、回车键、字母、数字及其他字符的个数(Ctrl+Z终止输入) ,希望对大家有所帮助,如果大家有任何疑问欢迎

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c 统计 个数
相关文章推荐