元音字母有大小写
整体思路,顺序度string,碰到元音字母就存起来,读完再顺序读一遍,这次碰到元音字母就用存起来的数组倒序替换
class Solution {
public:
string reverseVowels(string s) {
istringstream str(s);//将string字符绑定到输入流
vector<char>vowel;
char ch;
while(str>>ch){
if(isVowel(ch))
vowel.push_back(ch);//如果是元音字母就保存
}
for(int i = 0,j = 0;i != s.size();++i)
if(isVowel(s[i])){
s[i] = vowel[vowel.size() - 1 - j];
j += 1;
}
return s;
}
//判断是否为元音字母,含大小写
bool isVowel(char ch)
{
int flag = 0;
switch(ch){
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
flag = 1;
break;
default:
flag = 0;
break;
}
if(flag)
return 1;
else return 0;
}
};