Prashant Kumar
Prashant Kumar

Reputation: 318

Runtime error when reversing string in C

I don't understand why am I getting runtime error when I use s[j]!='\0' but when I use *temp!='\0' it works perfectly fine. Can anyone explain?

void reverseWords(char *s)
{
  char *word_begin = s;
  char *temp = s; /* temp is for word boundry */
  int i=0,j=0;

  while( s[j]!='\0' )
  {
    j++;
    if (s[j] == '\0')
    {
      reverse(s, i, j-1);
    }
    else if(s[j] == ' ')
    {
      reverse(s, i, j-1);
      i = j+1;
    }
  }     
}

Upvotes: 0

Views: 49

Answers (1)

Sami Kuhmonen
Sami Kuhmonen

Reputation: 31153

The error is not in that function. If you check your reverse function, you never increment i or decrement j so it will run forever.

A debugger is a helpful tool and would have shown this immediately.

Upvotes: 4

Related Questions