1. 程式人生 > >慕課第八週_練兵區_第二題

慕課第八週_練兵區_第二題

支付寶搜尋“ 610919172 ” 領紅包。
2 統計使用者輸入(4分)
題目內容:從鍵盤讀取使用者輸入直到遇到#字元,編寫程式統計讀取的空格數目、讀取的換行符數目以及讀取的所有其他字元數目。(要求用getchar()輸入字元)
程式執行結果示例1:Please input a string end by #:
abc def↙
jklm op↙
zkm #↙
space: 3,newline: 2,others: 15
程式執行結果示例2:Please input a string end by #:
hello friend!#↙
space: 1,newline: 0,others: 12
輸入提示資訊:“Please input a string end by #:\n”
輸入格式: getchar()
輸出格式: “space: %d,newline: %d,others: %d\n”
程式碼如下:
#include<stdio.h>
int main()
{
char ch;
int a,b,c;
a=b=c=0;
printf(“Please input a string end by #:\n”);
do
{
ch=getchar();
if(ch==’ ‘)
a++;
else if(ch==’\n’)
b++;
else
c++;
}while(ch!=’#’);
printf(“space: %d,newline: %d,others: %d\n”,a,b,c-1);
return 0;
}
支付寶搜尋“ 610919172 ” 領紅包。