leetcode 345. Reverse Vowels of a String

Original question can be found here.

My solution is as follows:

int isVowels(const char c);

char* reverseVowels(char* s)
{
        int len=strlen(s);
        int i=0, j=len-1;
        char tmp;
        while(1)
        {
                while(!isVowels(s[i]) && i<len)
                        ++i;

                while(!isVowels(s[j]) && j>=0)
                        --j;
                if(i<j)
                {
                        tmp=s[i];
                        s[i]=s[j];
                        s[j]=tmp;
                        ++i;
                        --j;
                }
                else
                        break;
        }
        return s;
}

int isVowels(const char c)
{
        if(c=='a' || c=='o' || c=='e' || c=='i' || c=='u' || c=='A' || c=='O' || c=='E' || c=='I' || c=='U')
                return 1;
        return 0;
}

2 thoughts on “leetcode 345. Reverse Vowels of a String”

Leave a Reply

Your email address will not be published. Required fields are marked *