输入10个字符,统计其中英文字母、空格或回车、数字字符和其他字符的个数C语言
时间: 2024-06-13 15:06:49 浏览: 18
以下是统计字符类型个数的C语言代码:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char c;
int letter = 0, space = 0, digit = 0, other = 0;
printf("请输入10个字符:\n");
for (int i = 0; i < 10; i++) {
c = getchar();
if (isalpha(c)) {
letter++;
} else if (isspace(c)) {
space++;
} else if (isdigit(c)) {
digit++;
} else {
other++;
}
}
printf("字母个数:%d\n空格或回车个数:%d\n数字个数:%d\n其他字符个数:%d\n", letter, space, digit, other);
return 0;
}
```
相关问题
用c语言实现 输入10个字符,统计其中英文字母、空格或回车、数字字符和其他字符的个数。
您好,根据您的要求,我可以回答您的问题。以下是用 C 语言实现输入 10 个字符,统计其中英文字母、空格或回车、数字字符和其他字符的个数的代码:
```c
#include <stdio.h>
int main() {
char c;
int letter = , space = , digit = , other = ;
printf("请输入 10 个字符:\n");
for (int i = ; i < 10; i++) {
scanf("%c", &c);
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
letter++;
} else if (c == ' ' || c == '\n') {
space++;
} else if (c >= '' && c <= '9') {
digit++;
} else {
other++;
}
}
printf("英文字母个数:%d\n", letter);
printf("空格或回车个数:%d\n", space);
printf("数字字符个数:%d\n", digit);
printf("其他字符个数:%d\n", other);
return ;
}
```
希望能够帮助您!
输入十个字符,统计其中中英文字母,空格或回车、数字字符和其它字符的个数,用c语言写
可以使用以下C语言代码实现:
```c
#include <stdio.h>
#include <ctype.h>
int main()
{
char input[10];
int letters = 0, spaces = 0, digits = 0, others = 0;
printf("请输入十个字符:\n");
fgets(input, 10, stdin);
for (int i = 0; input[i] != '\0'; i++)
{
if (isalpha(input[i]))
letters++;
else if (isspace(input[i]))
spaces++;
else if (isdigit(input[i]))
digits++;
else
others++;
}
printf("中英文字母:%d\n空格或回车:%d\n数字字符:%d\n其他字符:%d\n", letters, spaces, digits, others);
return 0;
}
```
运行程序后,输入十个字符,程序会统计不同类型字符的个数并输出。注意,输入的字符数量必须是十个,否则可能会出现不正确的统计结果。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)