c语言使用函数完成输入最多10个大小写字符,以回车符结束输入,输出其中的大写字母,
时间: 2024-05-27 18:14:03 浏览: 10
#include <stdio.h>
void printUpperCase(char *str) {
for(int i = 0; str[i] != '\0'; i++) {
if(str[i] >= 'A' && str[i] <= 'Z') {
printf("%c ", str[i]);
}
}
}
int main() {
char str[11]; // 最多10个字符,加上最后一个'\0'占用11个位置
int i = 0;
char c;
printf("请输入最多10个大小写字符,以回车符结束:\n");
while((c = getchar()) != '\n' && i < 10) {
str[i++] = c;
}
str[i] = '\0'; // 将最后一个位置赋为'\0'
printf("大写字母为:");
printUpperCase(str);
printf("\n");
return 0;
}
相关问题
用c语言编写程序:输入最多10个大小写字符,以回车符结束输入,输出其中的大写字母。 比如:输入 abcDeF 则输出 DF
```c
#include <stdio.h>
int main() {
char str[11]; // 定义一个字符数组,最多存储10个字符
int i, count = 0; // i用于循环计数,count用于记录大写字母个数
printf("请输入最多10个大小写字符,以回车符结束:\n");
scanf("%s", str); // 读取输入的字符
printf("其中大写字母有:");
for (i = 0; str[i] != '\0' && i < 10; i++) {
if (str[i] >= 'A' && str[i] <= 'Z') { // 判断是否为大写字母
printf("%c", str[i]); // 输出大写字母
count++; // 大写字母个数加1
}
}
if (count == 0) { // 如果没有大写字母
printf("无");
}
printf("\n");
return 0;
}
```
注意:
1. 使用字符数组存储输入的字符,可以通过循环遍历来判断是否为大写字母。
2. 由于字符数组中的每个元素都是字符类型,所以在读取输入时使用%s而非%c。
3. 在循环遍历字符数组时,需要注意数组下标不能超过数组长度,否则会出现数组访问越界的错误。因此可以使用str[i] != '\0'来判断是否到达字符数组的末尾。
C语言编程:输入一批字符(以回车结束),统计其中的大写英文字母的个数
以下是一个简单的 C 语言程序,用于输入一批字符并统计其中的大写英文字母个数:
```c
#include <stdio.h>
int main() {
char ch;
int count = 0;
printf("请输入一批字符(以回车结束):\n");
while ((ch = getchar()) != '\n') {
if (ch >= 'A' && ch <= 'Z') {
count++;
}
}
printf("大写英文字母个数为:%d\n", count);
return 0;
}
```
这个程序首先提示用户输入一批字符,然后通过 `getchar()` 函数逐个读取每个字符,如果读到的字符是大写英文字母则计数器加一。最后程序输出统计结果。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)