編寫一個程式,輸入一行字元,以回車結束,分別統計出其中的英文字母、空格、數字和其他字元的數
阿新 • • 發佈:2019-01-24
#include <stdio.h> int main() { int letter=0,space=0,digit=0,others=0; //宣告英文字母,空格,數字和其他字元的計數變數初始化為0 char c; //宣告接收字串的變數 while((c=getchar())!='\n'){ // 以回車符為結束的判斷標記 if(c==' ') // 檢測到空格 space++; else if(c>='0'&&c<='9') // 檢測到數字 digit++; else if((c>='a'&&c<='z')||(c>='A'&&c<='Z')) // 檢測到字母,要同時考慮字母的大小寫 letter++; else others++; } //輸出結果 printf("The number of letters is:%d\n",letter); printf("The number of spaces is:%d\n",space); printf("The number of digits is:%d\n",digit); printf("The number of other words is:%d\n",others); return 0; }