Why can't I pass test case 4?!?!


  • 0
    A
    #include <stdio.h>
    
    void reverseWords(char *s){
        static int c = 0;
        int n = 0;
        char* sp = s;
        
        if(*s != '\0'){
            // If s points to a space, move until it doesn't
            while(*s == ' '){
                s++;
                sp++;                
            }
    
            // If s points to a non-space, move sp until it finds the first space
            while(*s != ' ' && *s != '\0'){
                s++;
                n++;
            }
            
            if(*s != '\0'){
                c++;
                reverseWords(s);
            }
        }
        
        if(*sp == '\0' && n == 0){
            c--;
        }else{
            printf(("%.*s"), n, sp);
            if(c != 0){
                c--;
                printf("%c",' ');
            }
        }
    }

Log in to reply
 

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