本题目要求输入一个字符串,然后输出这个字符串中大写字母的个数。使用c语言 输入格式: 在一行中输入一个字符串。字符串长度不超过80。c语言
时间: 2024-12-12 12:19:39 浏览: 69
在 C 语言中,你可以使用 `ctype.h` 库中的函数来判断字符是否为大写字母,并遍历整个字符串来计算大写字母的数量。下面是一个简单的示例程序:
```c
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int count_uppercase_letters(char str[]) {
int uppercase_count = 0;
for(int i = 0; i < strlen(str); i++) {
if(isupper(str[i])) { // 判断字符是否为大写字母
uppercase_count++;
}
}
return uppercase_count;
}
int main() {
char input[81]; // 假设最多输入80个字符加上结束符'\0'
printf("请输入一个字符串: ");
fgets(input, sizeof(input), stdin); // 获取用户输入,包括换行符,需处理掉它
input[strcspn(input, "\n")] = '\0'; // 移除末尾的换行符
int result = count_uppercase_letters(input);
printf("大写字母的个数: %d\n", result);
return 0;
}
```
在这个程序中,`isupper()` 函数用于检查字符是否为大写字母。`count_uppercase_letters()` 函数接收字符串并返回大写字母的数量。在 `main()` 函数里,我们获取用户输入并调用该函数计算。
阅读全文
相关推荐


















