Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:
Given s = "hello", return "holle".
Example 2:
Given s = "leetcode", return "leotcede".
这道题的意思就是对换第一个和倒数第一个元音字母,对换第二个和倒数第二个元音字母。
string reverseVowels(string s) {
int l = s.length();
int i,j;
char temp;
for(i = 0,j = l-1; i < j; ){
if(isVowels(s[i]) && isVowels(s[j])){
temp = s[i];
s[i] = s[j];
s[j] = temp;
i++;
j--;
}
else if(!isVowels(s[i]) && isVowels(s[j])){
i++;
}
else if(!isVowels(s[j]) && isVowels(s[i])){
j--;
}
else{
i++;
j--;
}
}
return s;
}
bool isVowels(char x){
if(x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u' || x == 'A' || x == 'E' || x == 'I' || x == 'O' || x == 'U' )
return true;
else
return false;
}