C语言 练习统计各种字符的个数

统计字符 是很常见的 操作
下面按照 自己学习 进度 进行排序 ,涉及到的知识点 我会进行 总结
解法一 :最基础的 方法

#include <stdio.h>
main()
{
	char ch;
	int letter=0,space=0,digit=0,other=0;
	printf("Please input some characters:\n");
	while ((ch = getchar()) != '\n')
		{
			if (ch >= 'a' && ch <='z' || ch >= 'A' && ch <='Z')
				letter++;
			else if (ch =='\40')
				space++;
			else if (ch >='0'&&ch <='9')
				digit ++;
			else
				other++;
			
		}
	printf("char =%d,digit =%d,space=%d,other =%d",letter,digit,space,other);
}

方法二:使用字符数组

发布了26 篇原创文章 · 获赞 1 · 访问量 954

猜你喜欢

转载自blog.csdn.net/myxk_/article/details/104020069