題目:輸入一行字符,分別統計出其中英文字母、空格、數字和其它字符的個數,
程式分析:利用while陳述句,條件為輸入的字符不為'\n',
實體:
1 #include<stdio.h> 2 int main() 3 { 4 char c; 5 int letters=0,spaces=0,digits=0,others=0; 6 printf("請輸入一些字母:\n"); 7 while((c=getchar())!='\n') 8 { 9 if((c>='a'&&c<='z')||(c>='A'&&c<='Z')) 10 letters++; 11 else if(c>='0'&&c<='9') 12 digits++; 13 else if(c==' ') 14 spaces++; 15 else 16 others++; 17 } 18 printf("字母=%d,數字=%d,空格=%d,其他=%d\n",letters,digits,spaces,others); 19 return 0; 20 }
以上實體輸出結果為:
請輸入一些字母: www.kangyifan.com 123 字母=15,數字=3,空格=1,其他=2
感謝你的閱讀,請用心感悟!希望可以幫到愛學習的你!!分享也是一種快樂!!!請接力,,,
點擊查看原文,謝謝!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/24368.html
標籤:C
下一篇:C 實戰練習題目18
