String reverse using pointers

Solution 1:

You're losing your pointer to the beginning of the string, so when you print it out you're not starting from the first character, because str no longer points to the first character. Just put in a placeholder variable to keep a pointer to the beginning of the string.

void reverse(char *str)
{
  char *begin = str; /* Keeps a pointer to the beginning of str */
  char *rev_str = str;
  char temp;
  while(*str)
      str++;
  --str;

  while(rev_str < str)
  {
      temp = *rev_str;
      *rev_str = *str;
      *str = temp;   
      rev_str++;      
      str--;
  }
  printf("reversed string is %s\n", begin);
}