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

Download Application: Learn C Programming with Live Compiler

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

Flowchart : Program to Reverse a String

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
guest
0 Comments
Inline Feedbacks
View all comments

Join Our Newsletter Now

Get the latest tutorials, examples and projects updates.

Ads Block Detector Powered by codehelppro.com

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