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

 更新时间:2020年4月25日 17:28  点击:1765

具体代码如下所述:

#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终止输入) ,希望对大家有所帮助,如果大家有任何疑问欢迎

[!--infotagslink--]

相关文章