Courses

### Courses

Structure and Union

No Examples found for this topic - CodeHelpPro

Data Files

No Examples found for this topic - CodeHelpPro

# 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.

## Algorithm

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```

## Flowchart

This is the flowchart of this program

## 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
while(*sptr)
{
sptr++;
i++;
}

// storing the string str in rev in reverse order
while(i >= 0)
{
sptr--;
*rptr = *sptr;
rptr++;
i--;
}

//string should always end with '\0'
*rptr = '\0';
rptr = rev;

// storing the reverse string in the original string
while(*rptr)
{
*sptr = *rptr;
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``````
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments

### Join Our Newsletter Now

Get the latest tutorials, examples and projects updates.

!

### Ads

#### Ads Blocker Detected!!!

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