用c语言实现从键盘中输入一行字符,统计其中英文字母,数字,空格和其他字符的个数
时间: 2023-05-23 18:01:36 浏览: 93
当然可以,以下是一个简单的实现:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int alphabets = 0, digits = 0, spaces = 0, others = 0;
printf("请输入一行字符:");
fgets(str, 100, stdin);
for (int 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("英文字母个数:%d\n", alphabets);
printf("数字个数:%d\n", digits);
printf("空格个数:%d\n", spaces);
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语言实现了输入一行字符,然后统计其中英文字母、空格、数字和其他字符的个数。具体实现方法是通过一个while循环,每次读取一个字符,然后判断该字符是字母、数字、空格还是其他字符,并分别对应地增加相应的计数器。最后输出各个计数器的值即可。
需要注意的是,这段代码中使用了getchar()函数来读取字符,该函数每次只能读取一个字符。另外,该代码中使用了ASCII码来判断字符的类型,需要了解ASCII码表中各个字符的编码范围。
相关推荐
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)