輸入一行字元,統計其中的英文字元、數字字元、空格字元,以及其他字元的個數。請找出以下程式的錯誤,並改正之。
阿新 • • 發佈:2019-02-10
#include <stdio.h> #include <string.h> #define ARR_SIZE 80 main() { char str[ARR_SIZE]; int len, i; int letter=0,digit=0,space=0,other=0; printf("請輸入一個字串:"); gets(str); len = strlen(str); for (i=0; i<len; i++) { if (str[i]>='a'&&str[i]<='z' || str[i]>='A'&&str[i]<='Z') { letter ++; } else if (str[i]>='0'&&str[i]<='9') { digit ++; } else if (str[i]==' ' ) { space ++; } else other ++; } printf("英文字元數:%d\n", letter); printf("數字字元數:%d\n", digit); printf("空格數:%d\n", space); printf("其他字元數:%d\n", other); }