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);
}

方法二:使用字符數組

發佈了34 篇原創文章 · 獲贊 2 · 訪問量 4191
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章