How to do it…

  1. Enter a string to assign to the str string variable as follows:
printf("Enter a string: ");
scanf("%s", str);
  1. Set a pointer to point at the string, as demonstrated in the following code. The pointer will point at the memory address of the string's first character:
ptr1=str;
  1. Find the length of the string by initializing an n variable to 1. Set a while loop to execute when the pointer reaches the null character of the string as follows:
n=1;
while(*ptr1 !='\0')
{
  1. Inside the while loop, the following actions will be performed:
ptr1++;
n++;
  1. The pointer will be at the null character, so move the pointer one step back to make it point at the last character of the string as follows:
ptr1--;
  1. Set another pointer to point at the beginning of the string as follows:
ptr2=str;
  1. Exchange the characters equal to half the length of the string. To do that, set a while loop to execute for n/2 times, as demonstrated in the following code snippet:
m=1;
while(m<=n/2)
  1. Within the while loop, the first exchange operations take place; that is, the characters pointed at by our pointers are exchanged:
temp=*ptr1;
*ptr1=*ptr2;
*ptr2=temp;
  1. After the character exchange, set the second pointer to move forward to point at its next character, that is, at the second character of the string, and move the first pointer backward to make it point at the second to last character as follows:
ptr1--;
ptr2++;
  1. Repeat this procedure for n/2 times, where n is the length of the string. When the while loop is finished, we will have the reverse form of the original string displayed on the screen:
printf("Reverse string is %s", str);

The reversestring.c program for reversing a string using pointers is as follows:

#include <stdio.h>
void main()
{
char str[255], *ptr1, *ptr2, temp ;
int n,m;
printf("Enter a string: ");
scanf("%s", str);
ptr1=str;
n=1;
while(*ptr1 !='\0')
{
ptr1++;
n++;
}
ptr1--;
ptr2=str;
m=1;
while(m<=n/2)
{
temp=*ptr1;
*ptr1=*ptr2;
*ptr2=temp;
ptr1--;
ptr2++;;
m++;
}
printf("Reverse string is %s", str);
}

Now, let's go behind the scenes.