Courses

### Courses

Structure and Union

No Examples found for this topic - CodeHelpPro

Data Files

No Examples found for this topic - CodeHelpPro

# Program to Reverse a String

0 Suresh Chand June 2, 2021

In this example, We will learn c program to reverse a string using while loop. For this, You must have knowledge in following topics

## Algorithm

This is the algorithm of this program

```Step 1: Start
Step 2: Initialize string str, rev and interger i and j and count
Step 3: Takes string from user and store to str variable
Step 4: Assign count = 0 and i = 0
Step 5: Repeat this step until str[i] != \'\\0\'
Step 5.1: Increase count by 1
Step 6: j = count - 1
Step 7: Repeat this step until i < count
Step 7.1: rev[i] = str[j]
Step 7.2: Decrease j by 1
Step 8: Print rev
Step 9: End```

## Flowchart

This is the flowchart of this program

## CODE: Program to Reverse a String

Now, We will see the code to reverse a string using while loop. You must have knowledge of while loop before starting code.

``````#include <stdio.h>
int main(){

char str[1000], rev[1000];
int i, j, count = 0;

printf("Enter string to reverse : ");
gets(str);

//finding the length of the string
while (str[count] != '\0'){
count++;
}
j = count - 1;

//reversing the string
for (i = 0; i < count; i++){
rev[i] = str[j];
j--;
}

printf("\nOriginal String: %s", str);
printf("\nString After Reverse: %s", rev);
return 0;
}``````

The output of above program is

`````` Please Enter any String :  Suresh
String after Reversing : hseruS``````
Subscribe
Notify of
Inline Feedbacks