I can't finish it with C


  • 0
    P
    bool is_vowels(char c);
    char* reverseVowels(char* s) {
        int len = strlen(s);
        char *c = malloc(len);
        c[len] = '\0';
        int low = 0, high = len - 1;
        while(low < high) {
            while((!is_vowels(s[low]))  && low < high)
                c[low]  = s[low++];
            while((!is_vowels(s[high])) && low < high)
                c[high] = s[high--];
            c[high] = s[low];
            c[low++]  = s[high--];
        }
        return c;
    }
    
    bool is_vowels(char c) {
        return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U';
    }
    

    That't my code. And test is:

    "Trap a rat! Stare
    piper
    at Star apart."
    

    It said when reverseVowels is called, the parameter s(char *) is null.But with anyother test it can work well.


  • 0

    You didn't take null parameter into consideration. When s == NULL, it will break at first line.


  • 0
    P

    I know that,but the test isn't NULL.However the test(Trap ... rat ... ) makes s NULL.


  • 0
    M

    I wrote this in C but met with the same problem with you.

    Did you find the root cause finally?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.