Case " one. +two three? ~four !five- " error


  • 0
    W

    Dear all,

    this is my code review about the string " one. +two three? ~four !five- "

    the result on Leetcode will be "!five- ~four ehree? +two one.", however, when I used Code::block compiler will return correct answer.

    Can anyone help me to know what's problem. The solution below on C:

    void reverseWords(char *s) {

    volatile char c[2]="  ";
    volatile char tmp;
    volatile int i=0,j,k,l;
    volatile int slen=strlen(s);
    volatile int slen2;
    int moves;
    
    
    while(*(s+i)==' ')
    {
        i++;
    }
    
    if(*(s+i)==0) 
    {
        *s='\0';
    return;
    }
    
    j=0;
    while(j<=slen-1 && *(s+slen-1-j)==' ')
    {
        j++;
    }
    slen=slen-i-j;
    strncpy(s,s+i,slen);
    *(s+slen)='\0';
    

    // return;
    i=0;
    while(*(s+i)!=' ' && *(s+i)!=0)
    {
    i++;
    }
    if(i==slen) return;

    moves=slen+1;
    for(i=0;i<moves;i++)
    {
    k=i%2;
    l=(i+1)%2;
    c[k]=*(s+slen-1);

      if(c[l]==' '&& *s==' ')
        {
        
        slen--;
        continue;
        }
        
        for(j=0;j<slen-1;j++)
        {
          *(s+slen-1-j)=*(s+slen-2-j);
        }
    
        *s=c[l];
    }
    
    if(*s==' ')
    {
     strncpy(s,s+1,slen-1);
     slen--;
    }
    *(s+slen)='\0';
    
    for(i=0;i<slen/2;i++)
    {
        tmp=*(s+i);
        *(s+i)=*(s+slen-1-i);
        *(s+slen-1-i)=tmp;
    }
    

    //return;
    i=0;
    k=0;
    while(i<=slen)
    {
    if(*(s+i)==' ' || *(s+i)==0|| (s+i)=='\t')
    {
    slen2=i-k;
    for(j=0;j<(slen2/2);j++)
    {
    tmp=
    (s+j+k);
    (s+j+k)=(s+k+slen2-1-j);
    *(s+k+slen2-1-j)=tmp;
    }
    k=i+1;
    }
    i++;
    }
    }


  • 0

    @Whatamove Sorry, you posted in the wrong category. Please read the instructions on how to post in the correct category, thanks.


Log in to reply
 

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