c语言实现:输入一个字符串,统计英语字母、数字字符、其它字符的个数。
时间: 2024-05-09 09:15:34 浏览: 139
以下是C语言实现,可以使用`ctype.h`头文件中的函数判断字符类型:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int letter = 0, digit = 0, other = 0;
int i;
printf("请输入一个字符串:");
fgets(str, 100, stdin);
for (i = 0; str[i] != '\0'; i++) {
if (isalpha(str[i])) { // 判断是否为英文字母
letter++;
} else if (isdigit(str[i])) { // 判断是否为数字字符
digit++;
} else { // 其它字符
other++;
}
}
printf("字母个数:%d\n", letter);
printf("数字个数:%d\n", digit);
printf("其它字符个数:%d\n", other);
return 0;
}
```
以上代码中,`fgets()`函数用于从标准输入中获取字符串,`isalpha()`函数判断字符是否为英文字母,`isdigit()`函数判断字符是否为数字字符。
相关问题
C语言代码:输入一字符串(该字符串全部由小写字母组成),分别统计字符串中所包含的各个字符的个数。字符串长度不超过50。 输入格式: 输入一个字符串str。 输出格式: 按字母顺序输出每个字母的个数。
好的,以下是相应的C语言代码:
```
#include <stdio.h>
#include <string.h>
int main() {
char str[51];
int count[26] = {0};
int len, i;
printf("请输入字符串:\n");
scanf("%s", str);
len = strlen(str);
for (i = 0; i < len; i++) {
count[str[i] - 'a']++;
}
printf("各个字母出现的个数如下:\n");
for (i = 0; i < 26; i++) {
if (count[i] > 0) {
printf("%c:%d\n", i + 'a', count[i]);
}
}
return 0;
}
```
解析:
首先,我们定义了一个长度为51的字符数组str,以及一个长度为26的整型数组count(用于存放每个字母出现的次数)。然后,通过scanf函数获取输入的字符串。使用strlen函数获取输入字符串的长度len。我们遍历字符串str,并使用方括号运算符(数组下标)将字符转为数字做为下标,然后在count数组中对应元素加一。最后,我们遍历整个count数组,如果元素的值大于0,就输出对应的字母以及出现的次数。
输出结果如下:
```
请输入字符串:
hello
各个字母出现的个数如下:
e:1
h:1
l:2
o:1
```
用c语言编写一个代码:输入一串字符串,统计字母、数字、空格以及其他字符的个数,使用while语句
在C语言中,你可以使用while循环结合ASCII码来实现这个功能。这里是一个简单的示例,用户输入字符串,然后统计其中的字母、数字、空格和其他字符的数量:
```c
#include <stdio.h>
#include <ctype.h> // 包含处理字符类型函数
int main() {
char input[100];
int alpha_count = 0, digit_count = 0, space_count = 0, other_count = 0;
int i = 0;
while (input[i] != '\0') { // 遍历直到遇到字符串结尾
if (isalpha(input[i])) { // 判断是否为字母
alpha_count++;
} else if (isdigit(input[i])) { // 判断是否为数字
digit_count++;
} else if (isspace(input[i])) { // 判断是否为空格
space_count++;
} else { // 其他字符(如特殊字符)
other_count++;
}
i++; // 移动指针到下一个字符
}
printf("字母数量:%d\n", alpha_count);
printf("数字数量:%d\n", digit_count);
printf("空格数量:%d\n", space_count);
printf("其他字符数量:%d\n", other_count);
return 0;
}
`
在这个代码里,`isalpha()`、`isdigit()` 和 `isspace()` 分别用于检查字符是否为字母、数字和空格。如果不是这些,我们就认为它属于其他字符。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)