题目描述: 输入一字符串,该字符串包含字母、数字和其他字符。统计该字符串中数字、字母和其他字符出现的频率。(注意,其他字符包含空格)
输入描述: 一混合字符串,最大长度为200 各类字符出现频率
输出描述: 各类字符出现频率
样式输入: abcdefg9999
样式输出: ch:7 num:4 other:4
#include <stdio.h> // 引入标准输入输出库
#include <string.h> // 引入字符串处理库
#include <ctype.h> // 引入字符处理库
int main() {
char input[100]; // 定义一个字符数组,用于存储输入的字符串
fgets(input, sizeof(input), stdin); // 从标准输入读取一行字符串,存储到input数组中
int charCount = 0, numCount = 0, otherCount = 0; // 定义三个整型变量,分别用于统计字符、数字和其他字符的数量
for (int i = 0; i < strlen(input); i++) { // 遍历input数组中的每个字符
if (isalpha(input[i])) { // 如果当前字符是字母
charCount++; // 字符数量加1
} else if (isdigit(input[i])) { // 如果当前字符是数字
numCount++; // 数字数量加1
} else { // 如果当前字符既不是字母也不是数字
otherCount++; // 其他字符数量加1
}
}
printf("ch:%d\n", charCount); // 输出字符数量
printf("num:%d\n", numCount); // 输出数字数量
printf("other:%d", otherCount); // 输出其他字符数量
return 0; // 程序正常结束,返回0
}