c语言输入一个以回车结束的字符串(少于80个字符),统计其中大写字母的个数。
时间: 2023-06-10 19:08:55 浏览: 130
可以使用C语言标准库函数中的`fgets()`函数读取以回车结束的字符串,然后使用`strlen()`函数获取字符串长度,遍历字符串中每个字符,判断其是否为大写字母,最终统计大写字母的个数。
下面是示例代码:
```c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char str[80];
int len, count = 0;
printf("请输入一个少于80个字符的字符串:");
fgets(str, 80, stdin); // 从标准输入读取字符串
len = strlen(str); // 获取字符串长度
for (int i = 0; i < len; i++) {
if (isupper(str[i])) { // 判断字符是否为大写字母
count++;
}
}
printf("大写字母个数为:%d\n", count);
return 0;
}
```
注意:此代码中使用了标准库函数`isupper()`判断字符是否为大写字母,需要在程序开头加上`#include <ctype.h>`头文件。