【牛客】统计字符
⭐️ 题目描述
🌟 OJ链接:HJ40 统计字符
ps:
判断字符可以直接使用头文件自带的函数。
函数 | 作用 |
---|---|
iscntrl | 判断是否为控制字符 |
isspace | 判断是否为空白字符(空格、换页’\f’、换行’\n’、回车’\r’、制表符’'\t) |
isdigit | 判断是否为数字字符 |
islower | 判断是否为小写字母 |
isupper | 判断是否为大写字母 |
isalpha | 判断是否为字母 |
代码:
#include <stdio.h>
#include <ctype.h>
int main() {char str[1001] = {0};gets(str);int space = 0;int alpha = 0;int num = 0;int other = 0;int i = 0;while (str[i]) {if (isspace(str[i])) {space++;} else if (isalpha(str[i])) {alpha++;} else if (isdigit(str[i])) {num++;} else {other++;}i++;}printf("%d\n%d\n%d\n%d" , alpha , space , num , other);return 0;
}