c语言分别统计字符串中大写字母和小写字母的个数
时间: 2023-04-05 21:03:57 浏览: 295
这个问题可以回答。可以使用C语言中的字符处理函数,遍历字符串中的每个字符,判断其是否为大写字母或小写字母,然后分别统计个数即可。以下是示例代码:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int upper_count = 0, lower_count = 0;
printf("请输入一个字符串:");
fgets(str, sizeof(str), stdin);
for (int i = 0; str[i] != '\0'; i++) {
if (isupper(str[i])) {
upper_count++;
} else if (islower(str[i])) {
lower_count++;
}
}
printf("大写字母个数:%d\n", upper_count);
printf("小写字母个数:%d\n", lower_count);
return 0;
}
```
相关问题
C语言分别统计字符串中大写字母和小写字母的个数。例如,输入字符串s:“AAaaBBb123CCccccd”,则应输出结果:upper=6,lower=3。
在C语言中,你可以通过遍历字符串并使用字符处理函数来统计大写字母和小写字母的数量。这里是一个简单的示例程序:
```c
#include <stdio.h>
#include <ctype.h> // 包含处理字符类型的库函数
int main() {
char s[] = "AAaaBBb123CCccccd";
int upper_count = 0, lower_count = 0;
int i;
for (i = 0; s[i] != '\0'; i++) { // 遍历字符串直到遇到空字符'\0'
if (isupper(s[i])) { // 检查是否为大写字母
upper_count++;
} else if (islower(s[i])) { // 否则是小写字母
lower_count++;
}
}
printf("upper=%d, lower=%d\n", upper_count, lower_count);
return 0;
}
```
这个程序首先初始化两个计数器 `upper_count` 和 `lower_count`,然后逐个检查字符串中的每个字符。如果字符是大写字母,就增加 `upper_count`;如果是小写字母,则增加 `lower_count`。最后,它打印出大写字母和小写字母的总数量。
C语言输入一字符串,分别统计字符串中大写字母和小写字母的个数
以下是使用 C 语言编写的程序,统计字符串中大写字母和小写字母的个数:
```c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char str[100];
int length;
int uppercase = 0;
int lowercase = 0;
printf("请输入字符串:");
scanf("%s", str);
length = strlen(str);
// 统计大写字母和小写字母的个数
for (int i = 0; i < length; i++) {
if (isupper(str[i])) {
uppercase++;
} else if (islower(str[i])) {
lowercase++;
}
}
printf("大写字母的个数为:%d\n", uppercase);
printf("小写字母的个数为:%d\n", lowercase);
return 0;
}
```
程序首先要求用户输入字符串,然后使用 `strlen` 函数获取字符串的长度。接下来,使用 `for` 循环遍历字符串的每个字符,如果字符为大写字母,则将 `uppercase` 的值加 1;如果字符为小写字母,则将 `lowercase` 的值加 1。最后,输出大写字母和小写字母的个数。在这个程序中,我们使用了 `ctype.h` 中的函数 `isupper` 和 `islower` 来判断字符是否为大写字母或小写字母。
阅读全文
相关推荐













