Reverse String using Pointer

0 June 15, 2021

In this example, We will learn a program to Reverse String using Pointer. Before that, You must have knowledge of the following topics.

Download Application: Learn C Programming with Live Compiler


This is the algorithm of this program

Step 1: Start
Step 2: Declare string variables str and rev
Step 3: Declare pointer variables sptr and rptr
Step 4: *sptr = str and *rptr = rev
Step 5: Declare integer variables and assign -1
Step 6: Take input from user and store in str
Step 7: REPEAT this step until sptr is NULL
        Increase sptr by 1
        Increase i by 1
Step 8: REPEAT this step unitl i >= 0
        Decrease sptr by 1
        *rptr = *sptr
        Increase rptr by 1
        Decrease i by 1
Step 9: *rptr = \'\\0\' and rptr = rev
Step 10: REPEAT this step until rptr is NULL
        *sptr = *rptr;
        Increase sptr by 1
        Increase rptr by 1
Step 11: Print str
Step 12: End


This is the flowchart of this program

Flowchart : Reverse String using Pointer

CODE: Reverse String using Pointer

Now, We will see the code to reverse a string.

#include <stdio.h>

int main(){

    char str[100];
    char rev[100];
    char *sptr = str;
    char *rptr = rev;

    int i = -1;

    printf("\nEnter a string: ");
    scanf("%s", str);

    // storing the ending address of str in sptr

    // storing the string str in rev in reverse order
    while(i >= 0)
        *rptr = *sptr;
    //string should always end with '\0'
    *rptr = '\0'; 
    rptr = rev;

    // storing the reverse string in the original string
        *sptr = *rptr;

    // printing the reverse string
    printf("\nReverse of the string is: %s ", str);
    return 0;

The output of above program is

Enter a string: Hello

Reverse of the string is: olleH
Notify of
Inline Feedbacks
View all comments

Join Our Newsletter Now

Get the latest tutorials, examples and projects updates.



Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Disable Ad Block

Want Same System