Why is my solution for "Reverse Words in a string " not getting accepted


  • 0
    A

    This is my code for reversing words in the string which is working fine in my computer but is not accepted by the online judge.I am not including the main function in the online judge as they are only asking for the reverse function. I am getting this error :
    Wrong Answer :
    Input:
    " "
    Output:
    " "
    Expected:
    ""
    Stdout:

    #include<string.h>
    #include<stdio.h>
    
    char stack[100];
    int top=-1;
    
    void push(char c)
    {
        if(top>=100)
            return ; // overflow
        top+=1;
        stack[top]=c;
    }
    
    char pop()
    {
        if(top==-1)
            return('\0'); // underflow
        top-=1;
        return(stack[top+1]);
    }
    
    int isempty()
    {
        if(top==-1)
        return 1;
        else
        return 0;
    }
    
    void reverseWords(char *s) {
        int len=strlen(s);
        int ptr=len-1;
    
        while(ptr>=-1)
        {
            if(s[ptr]==' ' || ptr==-1)
            {
                while(!isempty())
                {
                    printf("%c",pop());
                }
                if(ptr!=-1)
                printf(" ");
            }
            else
            push(s[ptr]);
    
            ptr--;
        }
    
    }
    int main()
    {
        reverseWords("the sky is blue");
    }

Log in to reply
 

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