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”.
代碼:
//遍歷字符串,temp數組順序存儲元音字母;
//再遍歷字符串,碰到元音字母從temp數組最後元素往前替換
char* reverseVowels(char* s) {
if(strlen(s) <= 1) return s;
char *head = s;
char *temp = (char *)malloc(strlen(s) * sizeof(char));
int i = 0;
char str[11] = "aAeEiIoOuU";
while(*s != '\0')
{
//strchr() 在一個字符串中查找另一個字符串第一次出現的位置,找到返回這個字符指針,找不到返回NULL;
if(NULL != strchr(str, *s))
temp[i++] = *s;
s++;
}
s = head;
while(*s != '\0')
{
if(NULL != strchr(str, *s))
*s = temp[--i];
printf("%d\n", i);
s++;
}
return head;
}