入一行字符分别统计出其中英文字母、数字、空格和其他字符的个数。输入一行字符输出统计值样例输入 aklsjflj123 sadf918u324 asdf91u32oasdf;123样例输出23 16 2 4用c语言写
#include <stdio.h>
int main() {
char str[100];
scanf("%[^\n]", str);
int i = 0;
int letterCount = 0;
int digitCount = 0;
int spaceCount = 0;
int otherCount = 0;
while (str[i] != '\0') {
if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) {
letterCount++;
} else if (str[i] >= '0' && str[i] <= '9') {
digitCount++;
} else if (str[i] == ' ') {
spaceCount++;
} else {
otherCount++;
}
i++;
}
printf("%d %d %d %d\n", letterCount, digitCount, spaceCount, otherCount);
return 0;
}
``
原文地址: https://www.cveoy.top/t/topic/hBCv 著作权归作者所有。请勿转载和采集!