任意输入一串字符保存在字符数组中,求期中字母,数字及其他字符的个数。空格算其他字符。例如: char s[100];gets(s);比如输入了:my age is 28 结果显示为:字母7个,数字2个,其他3个。 1.代码比单元格长没有关系。2.每个变量要用一条输入语句。且必须按题目要求顺序输入。3.只能使用scanf()或者gets()输入
时间: 2024-02-09 11:13:28 浏览: 30
好的,可以用以下代码实现:
```c
#include <stdio.h>
#include <string.h>
int main() {
char s[100];
int letter_count = 0, digit_count = 0, other_count = 0;
printf("请输入一串字符:");
gets(s);
for (int i = 0; i < strlen(s); i++) {
if (s[i] >= 'a' && s[i] <= 'z' || s[i] >= 'A' && s[i] <= 'Z') {
letter_count++;
} else if (s[i] >= '0' && s[i] <= '9') {
digit_count++;
} else {
other_count++;
}
}
printf("字母%d个,数字%d个,其他%d个。\n", letter_count, digit_count, other_count);
return 0;
}
```
该程序使用了 `gets` 函数读取用户输入的字符串,然后通过遍历字符串中的每个字符,用条件判断语句判断每个字符是字母、数字还是其他字符,并分别统计它们的数量。最后输出统计结果。注意,该程序使用了不安全的 `gets` 函数,实际上应该使用 `fgets` 函数来更安全地读取用户输入。