题解 | #统计字符#
统计字符
https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5
#include <stdio.h>
#include <stdbool.h>
bool isStr(char inPut) {
if (inPut >= 'A' && inPut <= 'Z') {
return true;
} else if (inPut >= 'a' && inPut <= 'z') { //小写字母变换
return true;
}
return false;
}
bool isNum(char inPut) {
if (inPut >= '0' && inPut <= '9') {
return true;
}
return false;
}
bool isSpace(char inPut) {
if (inPut == ' ') {
return true;
}
return false;
}
//统计信息
void statisticsInf(char* str ) {
int strNum = 0; // 字符串个数
int spaceNum = 0; //空格数量
int num = 0;
int other = 0;
int len = strlen(str)-1;
for (int i = 0; i < len; i++) {
if (isStr(str[i]) == true) {
strNum++;
} else if (isNum(str[i]) == true) {
num++;
} else if (isSpace(str[i]) == true) {
spaceNum++;
} else {
other++;
}
}
printf("%d\r\n%d\r\n%d\r\n%d\r\n", strNum, spaceNum, num, other);
}
int main() {
char str[1001];
fgets(str, 1001, stdin);
statisticsInf(str);
}
查看11道真题和解析
