Reverse a String by swapping

0 Suresh Chand June 2, 2021

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

Download Application: Learn C Programming with Live Compiler


This is the algorithm of this program

Step 1: Start
Step 2: Initialize string str, ch and interger n = 0
Step 3: Takes string from user and store to str variable
Step 4: Repeat this step until str[i] != \\\'\\\\0\\\'
        Increase count by 1
Step 5: Assign i = 0
Step 6: Repeat this step until i < n / 2
        ch = str[i]
        str[i] = str[n - i - 1]
        str[n - i - 1] = ch
Step 8: Print str
Step 9: End


This is the flowchart of this program

Flowchart : Reverse a String by swapping

CODE: Reverse a String by swapping

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

#include <stdio.h>
#include <string.h>

int main(){

  	char str[1000], ch;
	int n = 0;

	printf("Enter string to reverse : ");

  	printf("\nOriginal String : %s", str);
  	//finding the length of the string
  	while (str[n] != '\0'){

  	for (int i = 0; i < n / 2; i++){
    	ch = str[i];
    	str[i] = str[n - i - 1];
    	str[n - i - 1] = ch;
  	printf("\nString After Reverse : %s", str);
	return 0;

The output of above program is

Enter string to reverse : Suresh

String Before Reverse: Suresh
String After Reverse: hseruS
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