c++输入一个字符串(少于80个字符),统计并输出其中元音字母(AEIOUaeiou)的个数(不区分大小写)。
时间: 2023-05-21 14:05:49 浏览: 134
我明白了,下面是我的回答:
```python
vowels = 'aeiou'
string = input('请输入一个字符串:')
count = 0
for char in string:
if char.lower() in vowels:
count += 1
print(f'其中元音字母的个数为:{count}。')
```
该程序可以统计一个输入字符串中元音字母的个数,不区分大小写。
相关问题
c++输入字符串,判断字符串中元音字母个数并输出
在C++中,你可以通过遍历字符串并对每个字符检查是否是元音字母(即'a', 'e', 'i', 'o', 'u'或其大写形式)来计算字符串中元音字母的数量。下面是一个简单的示例程序:
```cpp
#include <iostream>
#include <string>
bool is_vowel(char ch) {
std::string vowels = "aeiouAEIOU";
return vowels.find(ch) != std::string::npos;
}
int count_vowels(const std::string& str) {
int count = 0;
for (char ch : str) {
if (is_vowel(ch)) {
count++;
}
}
return count;
}
int main() {
std::string input;
std::cout << "请输入一个字符串: ";
std::getline(std::cin, input);
int vowel_count = count_vowels(input);
std::cout << "字符串中共有 " << vowel_count << " 个元音字母。\n";
return 0;
}
```
在这个程序中,`is_vowel` 函数用于检查单个字符是否是元音,`count_vowels` 函数则遍历整个字符串并计数元音。用户可以在 `main` 函数中输入字符串,程序会计算并输出其中元音字母的数量。
在C++中输入一个字符串(少于80个字符),统计并输出其中元音字母(AEIOUaeiou)的个数(不区分大小写)。
好的,以下是我的回答:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[80];
int count = 0;
printf("请输入一个字符串(不超过80个字符):");
fgets(str, 80, stdin);
for (int i = 0; str[i]; i++) {
if (tolower(str[i]) == 'a' || tolower(str[i]) == 'e' || tolower(str[i]) == 'i' || tolower(str[i]) == 'o' || tolower(str[i]) == 'u') {
count++;
}
}
printf("元音字母个数:%d\n", count);
return 0;
}
```
希望这个回答能够满足你的需求。
阅读全文
相关推荐













