//凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/
1 #include<stdio.h> 2 3 void main(){ 4 int c, letter=0, num=0, blank=0, tab=0, enter=0, other=0 ,i=0, sum=0; 5 printf("Please input a string:\n"); 6 while((c=getchar())!=EOF){ 7 sum++; 8 if(c==' '){ 9 ++blank; //空格键的个数 10 } 11 else if(c=='\t'){ 12 ++tab; //Tab键的个数 13 } 14 else if(c=='\n'){ 15 ++enter; //回车键的个数 16 } 17 else if((c>='A' && c<='Z') || (c>='a' && c<='z')){ 18 ++letter; //字母的个数 19 } 20 else if(c>='0' && c<='9'){ 21 ++num; //数字的个数 22 } 23 else ++other; //其他字符的个数 24 i++; 25 } 26 printf("There are %d characters\n", sum); 27 printf("blank=%d, Tab=%d, Enter=%d, letter=%d, number=%d ,other=%d\n",blank, tab, enter, letter, num, other); 28 29 }
结果为: