C语言:统计输入的一行字符中大写字母小写字母的个数
时间: 2024-05-06 20:21:41 浏览: 15
好的,我明白你的问题了。以下是对应的C语言代码,它可以统计输入的一行字符中大写字母和小写字母的个数:
```c
#include <stdio.h>
int main() {
char line[100];
int uppercase = 0, lowercase = 0, i = 0;
printf("请输入一行字符:");
fgets(line, 100, stdin);
while (line[i] != '\0') {
if (line[i] >= 'A' && line[i] <= 'Z')
uppercase++;
else if (line[i] >= 'a' && line[i] <= 'z')
lowercase++;
i++;
}
printf("大写字母个数为:%d\n", uppercase);
printf("小写字母个数为:%d\n", lowercase);
return 0;
}
```
上面的代码中,我们首先通过`fgets()`函数从标准输入中读取一行字符,并将它存储在`line`数组中。然后,我们使用一个`while`循环遍历`line`数组中的每一个字符,如果当前字符是大写字母,则把`uppercase`计数器加1,如果是小写字母,则把`lowercase`计数器加1。最后,我们输出统计结果。
我希望这个代码对你有所帮助,如果你还有其他问题,请随时提出。
相关问题
C语言统计输入的字符串中大写字母和小写字母的个数
可以使用C语言的字符操作函数isupper和islower来判断一个字符是否为大写字母或小写字母,然后统计出现次数即可。
以下是示例代码:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int upper_count = 0, lower_count = 0, i = 0;
printf("请输入字符串:");
fgets(str, 100, stdin);
while (str[i] != '\0') {
if (isupper(str[i])) {
upper_count++;
} else if (islower(str[i])) {
lower_count++;
}
i++;
}
printf("大写字母个数:%d\n", upper_count);
printf("小写字母个数:%d\n", lower_count);
return 0;
}
```
在这个示例中,我们使用了fgets函数来从标准输入读取字符串,然后遍历字符串中的每个字符,使用isupper和islower函数判断是否为大写字母或小写字母,最后统计出现次数并输出。
C语言统计输入的字符串中大写字母和小写字母的个数while语句
下面是一个 C 语言程序,使用 while 循环统计输入的字符串中大写字母和小写字母的个数:
```c
#include <stdio.h>
int main() {
char str[100];
int i = 0, upper = 0, lower = 0;
printf("Enter a string: ");
scanf("%s", str);
while (str[i] != '\0') {
if (str[i] >= 'A' && str[i] <= 'Z') {
upper++;
} else if (str[i] >= 'a' && str[i] <= 'z') {
lower++;
}
i++;
}
printf("Number of uppercase letters: %d\n", upper);
printf("Number of lowercase letters: %d\n", lower);
return 0;
}
```
程序首先提示用户输入一个字符串,并使用 scanf 函数将字符串存储在字符数组 str 中。然后使用 while 循环遍历字符串中的每个字符,如果字符是大写字母,则将大写字母计数器 upper 加 1;如果字符是小写字母,则将小写字母计数器 lower 加 1。最后程序输出统计结果。