c语言输入一行字符,分别求出其中的英文字母空格数字和其它个数
时间: 2023-11-25 18:48:33 浏览: 44
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int letter = 0, space = 0, digit = 0, other = 0;
fgets(str, 100, stdin);
for (int i = 0; str[i] != '\0'; i++) {
if (isalpha(str[i])) {
letter++;
} else if (isspace(str[i])) {
space++;
} else if (isdigit(str[i])) {
digit++;
} else {
other++;
}
}
printf("字母个数:%d\n空格个数:%d\n数字个数:%d\n其他字符个数:%d\n", letter, space, digit, other);
return 0;
}
```
--相关问题--:
1. 如何判断一个字符是英文字母、空格或数字?
2. 除
相关问题
用C语言输入一行字符分别统计出其中英文字母空格数字和其他字符的个数
好的,这是一个经典的问题,以下是代码实现:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int i, alphabets, digits, spaces, others;
alphabets = digits = spaces = others = 0;
printf("请输入一行字符:\n");
fgets(str, sizeof(str), stdin);
for (i = 0; str[i] != '\0'; i++) {
if (isalpha(str[i])) {
alphabets++;
} else if (isdigit(str[i])) {
digits++;
} else if (isspace(str[i])) {
spaces++;
} else {
others++;
}
}
printf("输入的字符串中包含:\n");
printf("%d 个字母\n", alphabets);
printf("%d 个数字\n", digits);
printf("%d 个空格\n", spaces);
printf("%d 个其他字符\n", others);
return 0;
}
```
希望这个回答能够帮到你!
c语言输入一行字符分别统计出其中英文字母,字符,空格,数字和其它字符的个数
这是三种不同的C语言程序,用于输入一行字符并统计其中英文字母、字符、空格、数字和其他字符的个数。这三种方法分别是常规计算、普通函数调用和指针函数调用。
常规计算方法使用while循环逐个读取输入的字符,并使用if语句判断字符的类型,最后输出各种字符的个数。
普通函数调用方法定义了一个count函数,该函数接受一个字符数组作为参数,并使用for循环逐个读取数组中的字符,然后使用if语句判断字符的类型,最后输出各种字符的个数。在主函数中,使用gets函数读取输入的字符,并将其作为参数传递给count函数。
指针函数调用方法与普通函数调用方法类似,但使用指针来遍历字符数组。在主函数中,使用while循环逐个读取输入的字符,并将其存储在字符数组中,然后将该数组作为参数传递给count函数。