c语言编写程序,输入一串字符,分别统计其中的英文字母、空格、数字和其他字符的个数
时间: 2024-03-22 08:33:19 浏览: 30
c
#include <stdio.h>
#include <ctype.h>
int main()
{
char str[100];
int i, letters = 0, space = 0, digit = 0, others = 0;
printf("请输入一串字符:");
fgets(str, 100, stdin);
for (i = 0; str[i] != '\0'; i++)
{
if (isalpha(str[i]))
{
letters++;
}
else if (isspace(str[i]))
{
space++;
}
else if (isdigit(str[i]))
{
digit++;
}
else
{
others++;
}
}
printf("字母个数:%d\n", letters);
printf("空格个数:%d\n", space);
printf("数字个数:%d\n", digit);
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
```
输入任意字符串,分别统计其中英文字母、空格、数字和其他字符的个数 C语言
以下是C语言统计字符串中英文字母、空格、数字和其他字符个数的代码:
```c
#include <stdio.h>
#include <ctype.h>
void count(char str[]) {
int ch = 0, s = 0, d = 0, o = 0;
for (int i = 0; str[i] != '\0'; i++) {
if (isalpha(str[i])) {
ch++;
} else if (isspace(str[i])) {
s++;
} else if (isdigit(str[i])) {
d++;
} else {
o++;
}
}
printf("char=%d space=%d digit=%d others=%d\n", ch, s, d, o);
}
int main() {
char str[100];
printf("请输入字符串:");
fgets(str, 100, stdin);
count(str);
return 0;
}
```
代码说明:
- `count`函数用于统计字符串中英文字母、空格、数字和其他字符的个数,参数为一个字符数组。
- `isalpha`函数用于判断一个字符是否为字母。
- `isspace`函数用于判断一个字符是否为空格。
- `isdigit`函数用于判断一个字符是否为数字。
- `fgets`函数用于从标准输入流中读取字符串。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)