Reverse string content using its pointer : Pointer String « Pointer « C / ANSI-C






Reverse string content using its pointer

Reverse string content using its pointer
  
#include <stdio.h>
#include <string.h>

int main(void)
{
  char str1[] = "Pointers are fun and hard to use";
  char str2[80], *p1, *p2;

  /* make p point to end of str1 */
  p1 = str1 + strlen(str1) - 1;

  p2 = str2;

  while(p1 >= str1)
    *p2++ = *p1--;

  /* null terminate str2 */
  *p2 = '\0';

  printf("%s %s", str1, str2);

  return 0;
}


           
       








Related examples in the same category

1.split an entry of the form Last/First into two parts