目标:

输入一行字符,统计其中各种字符的个数。

具体代码:

#include<stdio.h>#include<stdlib.h>#include<string.h>#define M 1024void main() {char str[M];fgets(str, M, stdin);int space = 0;int letter = 0;int num = 0;int other = 0;for (int i = 0; i < (int)strlen(str); ++i) {if (str[i] == ' ') {space += 1;}else if (str[i] > 64 && str[i] < 91 || str[i]>96 && str[i] < 123)  {letter += 1;}else if (str[i] > 47 && str[i] < 58)  {num += 1;}else {if (str[i] != '\n') {//因为fgets()函数会在末尾自动加上\n,影响判断结果,需要判断是否为换行符other += 1;}}}printf("空格的个数为:%d\n", space);printf("英文字母的个数为:%d\n", letter);printf("数字的个数为:%d\n", num);printf("其他字符的个数为:%d\n", other);system("pause");}

注意:fgets()函数会在字符串末尾(\0前)读入我们在键盘上敲的回车即换行符\n。

运行结果如下:

5704ecf534b3f2a07a729bf46dea266.png

推荐教程:c语言教程

更多相关文章

  1. c语言如何定义字符串数组
  2. c语言字符数组与字符串应用方法是什么?
  3. C语言怎么定义字符串数组
  4. C语言中的字符串比较函数是什么
  5. C语言中输出字符串用什么符号
  6. C++中字符串比较函数strcmp怎么用?
  7. c++如何比较两个字符串?
  8. c语言中将一个字符串转换到整型数据类型的函数是什么?
  9. 在什么语言中字符串以\0标志字符串的结束?

随机推荐

  1. android studio在模拟器上的中文乱码问题
  2. 几种常见的android Runtime异常
  3. Android中使用ALSA声卡
  4. Android 测试工具集02
  5. Android之拨号器
  6. Android 深入解析用户界面(四)
  7. android中log知识总结
  8. android notes(1)
  9. android 自定义组件圆形边框
  10. Ubuntu 将adb加入环境变量