垃圾PTA:7-2 統計數字字元和空格
阿新 • • 發佈:2020-11-13
本題要求編寫程式,輸入一行字元,統計其中數字字元、空格和其他字元的個數。建議使用switch語句編寫。
輸入格式:輸入在一行中給出若干字元,最後一個回車表示輸入結束,不算在內。
輸出格式:在一行內按照 blank = 空格個數, digit = 數字字元個數, other = 其他字元個數 的格式輸出。請注意,等號的左右各有一個空格,逗號後有一個空格。
輸入樣例:在這裡給出一組輸入。例如:
Reold 12 or 45T 輸出樣例:在這裡給出相應的輸出。例如:
blank = 3, digit = 4, other = 8
直接放程式碼:
#include <stdio.h> intmain() { char c; int letters=0,space=0,digit=0,other=0; while ((c=getchar())!='\n') { if (c >= 'a'&&c <= 'z' || c >= 'A'&&c <= 'Z') { other++; } else if (c == ' ') { space++; }else if (c >= '0'&&c <= '9') { digit++; } else { other++; } } printf("blank = %d, digit = %d, other = %d",space,digit,other); return 0; }
贈人玫瑰手有餘香~
NO THANKS!不用謝~
喜歡記得關注,評論~感謝各位大佬