输入一行字符串(少于100个字符),包含空格和特殊符号等。 统计并输出其中大写辅音字母的个数。大写辅音字母是指除'A' , 'E' , 'I' , ' O ' , ' U '以外的大写字母。试编写相应程序。
时间: 2024-10-17 18:03:15 浏览: 109
要统计输入字符串中大写辅音字母的个数,我们可以遍历整个字符串,检查每个字符是否为大写字母并且不是'A', 'E', 'I', 'O', 或 'U'。如果是,就增加计数器。以下是一个简单的C语言程序示例:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char input[101]; // 假设最多100个字符,包括空格和特殊符号
int count = 0; // 初始化大写辅音字母计数器
printf("请输入一行字符串(少于100个字符):");
fgets(input, sizeof(input), stdin); // 从标准输入读取字符串,不包括换行符
for (size_t i = 0; i < strlen(input); i++) { // 遍历字符串中的每个字符
if (isupper(input[i]) && input[i] != 'A' && input[i] != 'E' && input[i] != 'I' && input[i] != 'O' && input[i] != 'U') {
count++; // 如果找到大写辅音,计数加一
}
}
printf("大写辅音字母的个数: %d\n", count);
return 0;
}
```
当你运行这个程序时,它会提示你输入一行字符串,然后计算并输出其中的大写辅音字母数量。
相关问题
输入一个以回车结束的字符串(少于80 个字符),统计并输出其中大写辅音字母的个数(大写辅音字母:除A、E、I、O、U 以外的大写字母)。c语言
以下是统计大写辅音字母个数的 C 语言代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[80];
int len, count = 0;
printf("请输入字符串(少于80个字符):\n");
fgets(str, 80, stdin);
len = strlen(str);
for (int i = 0; i < len; i++) {
if (str[i] >= 'A' && str[i] <= 'Z') { // 判断是否为大写字母
if (str[i] != 'A' && str[i] != 'E' && str[i] != 'I' && str[i] != 'O' && str[i] != 'U') { // 判断是否为辅音字母
count++;
}
}
}
printf("大写辅音字母的个数为 %d\n", count);
return 0;
}
```
代码思路:
1. 定义字符数组 `str` 存储输入的字符串。
2. 用 `fgets()` 函数读取用户输入的字符串,避免出现空格等问题。
3. 用 `strlen()` 函数获取字符串的长度。
4. 循环遍历字符串,判断每个字符是否为大写字母,如果是则判断是否为辅音字母,如果是则计数器 `count` 加一。
5. 最后输出大写辅音字母的个数。
统计大写辅音字母:输入一个以回车结束的字符串(少于 80 个字符),统计并输出其中大写辅音字母的个数。大写辅音字母是指除'A'、'E'、'I'、'O'、'U'以外的大写字母。试编写相应程序。
要编写一个C++程序来统计给定字符串中的大写辅音字母,你可以使用`std::string`容器和`std::set`来存储不包含'A', 'E', 'I', 'O', 'U'的大写字母。然后遍历字符串,如果字符是大写字母并且不在这个集合里,就增加计数器。以下是完整的代码示例:
```cpp
#include <iostream>
#include <string>
#include <set>
int count_consonants(const std::string& input) {
std::set<char> vowels {'A', 'E', 'I', 'O', 'U'};
int consonant_count = 0;
for (char c : input) {
if (isupper(c) && vowels.find(c) == vowels.end()) {
consonant_count++;
}
}
return consonant_count;
}
int main() {
std::string str;
std::cout << "Enter a string (less than 80 characters): ";
getline(std::cin, str);
// Check that the input is valid
if (str.length() >= 80) {
std::cerr << "Input exceeds 80 characters.\n";
return 1;
}
int result = count_consonants(str);
std::cout << "Number of uppercase consonants: " << result << "\n";
return 0;
}
```
在这个程序中,我们首先定义了一个包含元音的大写字母集合`vowels`。然后在`count_consonants`函数中,我们遍历输入字符串,检查每个字符是否是大写字母且不在`vowels`集合中。如果是,我们就增加`consonant_count`。
在`main`函数中,用户被提示输入字符串,然后调用`count_consonants`函数获取结果并显示在屏幕上。如果输入超过80个字符,我们会发出错误信息。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.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)