c语言 输入一行字符,统计英文字母,空格,数字,其他字符的个数
时间: 2023-05-20 12:01:12 浏览: 81
以下是 C 语言的代码,可以实现输入一行字符,统计英文字母,空格,数字,其他字符的个数:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int i, letters = 0, spaces = 0, digits = 0, others = 0;
printf("请输入一行字符:\n");
fgets(str, sizeof(str), stdin);
for (i = 0; str[i] != '\0'; i++) {
if (isalpha(str[i])) {
letters++;
} else if (isspace(str[i])) {
spaces++;
} else if (isdigit(str[i])) {
digits++;
} else {
others++;
}
}
printf("英文字母个数:%d\n", letters);
printf("空格个数:%d\n", spaces);
printf("数字个数:%d\n", digits);
printf("其他字符个数:%d\n", others);
return 0;
}
```
希望能对你有所帮助!
相关问题
c语言输入一行字符统计其中英文字母、数字、空格和其他字符的个数
以下是C语言输入一行字符统计其中英文字母、数字、空格和其他字符的个数的代码实现:
```c
#include <stdio.h>
#include <ctype.h>
int main()
{
char str[100];
int i, letters = 0, digits = 0, spaces = 0, others = 0;
printf("请输入一行字符:\n");
fgets(str, sizeof(str), stdin);
for (i = 0; str[i] != '\0'; i++)
{
if (isalpha(str[i])) // 判断是否为字母
{
letters++;
}
else if (isdigit(str[i])) // 判断是否为数字
{
digits++;
}
else if (isspace(str[i])) // 判断是否为空格
{
spaces++;
}
else // 其他字符
{
others++;
}
}
printf("字母个数:%d\n", letters);
printf("数字个数:%d\n", digits);
printf("空格个数:%d\n", spaces);
printf("其他字符个数:%d\n", others);
return 0;
}
```
c语言输入一行字符分别统计其中英文字母 空格 数字 和其他字符的个数
好的,我知道了。以下是 c 语言统计字符的代码:
```
#include <stdio.h>
#include <ctype.h>
int main() {
char input;
int letters = 0, digits = 0, spaces = 0, others = 0;
printf("请输入一行字符:\n");
while ((input = getchar()) != '\n') {
if (isalpha(input)) {
letters++;
} else if (isdigit(input)) {
digits++;
} else if (isspace(input)) {
spaces++;
} else {
others++;
}
}
printf("字母个数:%d\n", letters);
printf("数字个数:%d\n", digits);
printf("空格个数:%d\n", spaces);
printf("其他字符个数:%d\n", others);
return 0;
}
```
输入输出示例:
```
请输入一行字符:
Hello World 123!
字母个数:10
数字个数:3
空格个数:2
其他字符个数:1
```