Program To Reverse a String in C using Pointer

Introduction

In this post, I am going to write a program to reverse a string in c using pointer. Reversing a string means this program will reverse characters of that string. This program can be done in many ways. But, here I am using pointer only.

Also Read: Switch Case in C Program to Calculate Area of Circle and Triangle

We know pointer is a variable where we can store the address of another variable. Now, before writing a program to reverse a string in c using pointer, let us see the expected output for this program.

Expected Output

Enter a String
ABCD EFG
Original String: ABCD EFG
Reverse String: GFE DCBA

This is a very simple program. Simply we are asking the user to enter a string. Whatever he will enter, this program will reverse that string.

Now, let us see the actual program.

Program To Reverse a String in C using Pointer

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
    char str1[100],str2[100];
    char *p1,*p2;
    printf("Enter a String\n");
    gets(str1);
    p1=str1+strlen(str1)-1;
    p2=str2;
    while(p1>=str1)
    {
       *p2=*p1;
        p2++;
        p1--;
    }
    *p2='\0';
    printf("Original String: %s\n",str1);
    printf("Reverse String: %s",str2);
    return 0;
}

Now, I am going to explain this program in a simple way.

Also Read: C Program to Print Multiples of 5 using do while loop

Variable Declaration and Input/Output

Here, I have declared two variables. Two normal variables and two pointer variables.

Program To Reverse a String in C using Pointer Variable Declaration
Variable declaration with output

In the above image, I am assuming memory locations for these variables str1, str2, p1 and p2. These locations are 10, 200, 400 and 410 for str1, str2, p1 and p2 respectively. These addresses can be anything. I have made it so simple for better understanding.

There is a printf() statement which is displaying the message “Enter a String” on the console. Suppose user has entered the string “ABCD EFG”.

Also Read: C Program to Remove Zeros from a number

This string will be stored in the variable str1 using the gets() function. You can also see how the string is stored in the variable str1. The first character is stored at location 10 and the last character is stored at location 17. At the end of this string, there is a null character. I have not mentioned it here.

Initialization of Pointer Variables

program to reverse a string in c using pointer - processing part 1
Initialization of Pointer Variables

Now, we need to initialize the pointer variables p1 and p2.

p1=str1+strlen(str1)-1;

In the variable p1, we are going to store the location of the last character in the string str1. Here, str1 means the starting address of the string or we can say the base address of the string. In simple words, base address means the address of the first character. In this case, this address is 10.

The function strlen() counts the total number of characters in the string. It will return the length of the string. This is a string handling function. So, strlen(str1)-1 will count the number of characters from the string str1 and subtract 1 from this value. Why? Because the first index for the array always starts from 0. Therefore, p1 = 10 + 8 – 1 = 17.

The variable p1 have the address of the last character from the string. Now, see the next statement.

p2=str2;

Here, str2 means the starting address of the string variable i.e. 200. Currently, there is nothing in the variable str2. We are going to store the output of our program in this variable i.e. reversed string. You can see, we are doing these using pointer variables.

Also Read: The while loop in C Programming

while loop

This is important part of the program to reverse a string in c.

First Iteration

program to reverse a string in c while loop
program to reverse a string in c using pointer – first iteration

As we know that the value of p1 is 17 and str is also 17. In the while loop, we are comparing the values of p1 and str. If the value of p1 is greater than or equal to str1, then its body of this while loop will be executed.

Here, the condition is true and following steps will be executed.

  1. *p2 = *p1; Here, value at the address which is stored in the pointer variable p1 i.e. G will be stored at the address which is stored in the pointer variable p2 i.e. at 200. You can see the above diagram.
  2. p2++; Now, we have started to reverse a string. In this step, we are incrementing the value of p2 by 1 because the next character will be stored at the next location.
  3. p1–; We will decrement the address by 1 because we need the character at the second last address.

These three steps will be repeated until the condition in the while loop become false.

Also Read: Reverse a Number using getchar and putchar function in c

Second Iteration

In the second iteration, the value of p1 is 16 but the value of str is the same because we are not incrementing or decrementing the value of str. Again the condition in the while loop is true.

program to reverse a string in c using pointer second iteration
program to reverse a string in c using pointer – second iteration

Third Iteration

program to reverse a string in c using pointer  third iteration
program to reverse a string in c using pointer – third iteration

Fourth Iteration

In this iteration, blank space is read. Blank space is also a character. The output will look like the same as the above diagram only the values of p1 and p2 will be changed i.e. p2 = 204 and p1=13.

Also Read: C Program to Print Numbers Except Multiples of n

Fifth Iteration

program to reverse a string in c using pointer fifth iteration
program to reverse a string in c using pointer – fifth iteration

Sixth Iteration

program to reverse a string in c using pointer – sixth iteration

Seventh Iteration

program to reverse a string in c using pointer seventh iteration
program to reverse a string in c using pointer – seventh iteration

Last Iteration

This is the last iteration because the value of p1 has become 10 which is equal to the value of the variable str. In the next iteration, the value of p1 will become 9 and the condition in the while loop will become false and the loop will be terminated.

Also Read: C Program to Remove White Spaces and Comments from a File

program to reverse a string in c using pointer last iteration
program to reverse a string in c using pointer – last iteration

In this way, we have written a program in c to reverse a string using a pointer. I hope you have understood this program. I have also made a video on this topic in the Hindi language.

If you have any difficulty in understanding this program then please feel free to contact me.

Thank you.

Some Important C Programs

  1. Program in C to Find Longest Line in a File
  2. Palindrome in C using Pointers
  3. Insert and Delete element in Array in C using switch case
  4. C Program to Add Alternate Elements of a 2D Array
  5. Arrays in C for Complete Beginners
  6. C Program to Find Area of a Circle using Preprocessor

Leave a Comment