c++字符串IO题目 Tom从小就对英文字母非常感兴趣,尤其是元音字母(a,e,i,o,u,A,E,I,O,U),他在写作文的时候都会把元音字母写成大写的,辅音字母则都写成小写, 你试试把一个句子翻译成他写作文的习惯吧。 请使用字符串流解决该问题,需要有main函数,要有注释(最好英文) 输入一个字符串S(长度不超过100,只包含大小写的英文字母和空格)。 输出 按照习惯输出翻译后的字符串S。 样例1 输入(输入末尾有空格): Who Love Solo 输出(输出末尾有空格): whO lOvE sOlO
时间: 2024-02-16 11:59:37 浏览: 158
C++检测字母字符串中各元音字母数量
Here is the code:
```c++
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
string line;
getline(cin, line); // read a line of input from user
istringstream iss(line); // create a string stream from the line
string word;
bool first = true; // a flag to indicate if it's the first word in a sentence
while (iss >> word) { // read a word from the string stream
if (!first) { // if it's not the first word in a sentence, add a space before it
cout << " ";
}
for (char& c : word) { // convert each character in the word
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'
|| c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
c = toupper(c); // convert a vowel to uppercase
} else {
c = tolower(c); // convert a consonant to lowercase
}
}
cout << word; // output the converted word
first = false; // set the flag to false
}
cout << endl; // output a newline at the end
return 0;
}
```
This program reads a line of input from the user, and then uses a string stream to read each word from the line. For each word, it converts each character to uppercase if it's a vowel, and to lowercase otherwise. It outputs the converted word, with a space before it if it's not the first word in a sentence. Finally, it outputs a newline at the end.
阅读全文