C program to Reverse a String

How to write a C program to Reverse a String without using the strrev function with an example? To demonstrate this, we are going to use For Loop, While Loop, Functions, and Pointers.

C program to Reverse a String using with a temporary array

This program allows the user to enter any character array. Next, it will use the For Loop to iterate each character and save the characters in inverse order.

#include <stdio.h>
#include <string.h>
 
int main()
{
  	char Str[100], RevStr[100];
  	int i, j, len;
 
  	printf("\n Please Enter any Text :  ");
  	gets(Str);
  	
  	j = 0;
  	len = strlen(Str);
 
  	for (i = len - 1; i >= 0; i--)
  	{
  		RevStr[j++] = Str[i];
  	}
  	RevStr[i] = '\0';
 
  	printf("\n And the Result of It is = %s", RevStr);
  	
  	return 0;
}
C program to Reverse a String without using strrev

From the above C Programming screenshot, you can observe that,

str[] = Hello
j = 0
len = strlen(Str) = 5.

The C strlen function is used to find the length of the input string in C. Let’s use that length in the programming flow.

Please refer to the C strrev function.

C For Loop First Iteration: for (i = len – 1; i >= 0; i–)
=> for (i = 5 – 1; 4 >= 0; 4–)
The condition (4 >= 0) is True.
RevStr[j++] = Str[i]
RevStr[0] = Str[4] = 0

Second Iteration: for (i = 4 – 1; 3 >= 0; 3–)
The condition (3 >= 0) is True.
RevStr[1] = Str[3] = l

Third Iteration of a C program to Reverse a String: for (i = 3 – 1; 2 >= 0; 2–)
The condition (2 >= 0) is True.
RevStr[2] = Str[2] = l

Fourth Iteration: for (i = 2 – 1; 1 >= 0; 1–)
The condition (1 >= 0) is True.
RevStr[3] = Str[1] = e

Fifth Iteration: for (i = 1 – 1; 0 >= 0; 0–)
The condition (0 >= 0) is True.
RevStr[4] = Str[0] = H

Next, the condition inside the For Loop will fail.

String reverse using for loop

Instead of storing the results in a separate array, we directly printed the string characters in reverse order in this program.

#include <stdio.h>
#include <string.h>
 
int main()
{
  	char Str[100];
  	int i, ln;
 
  	printf("\n Before :  ");
  	gets(Str);
  	
  	ln = strlen(Str);
  	
 	printf("\n After : "); 
  	for (i = ln - 1; i >= 0; i--)
  	{
  		printf("%c", Str[i]);
  	}

  	return 0;
}
Before : Tutorial Gateway
After: yawetaG lairotuT

Use temporary variable to reverse a string

In this example, we use the temp variable to change the character’s location. It is something like a C program to reverse a string using the swapping technique.

/* using temp variable */
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char Str[100], temp;
  	int i, j, len;
 
  	printf("\n Actual :  ");
  	gets(Str);
  	
  	len = strlen(Str) - 1;
 
  	for (i = 0; i < strlen(Str)/2; i++)
  	{
  		temp = Str[i];
  		Str[i] = Str[len];
  		Str[len--] = temp;
  	}
 
  	printf("\n Result = %s", Str);
  	
  	return 0;
}
 Actual :  C Programming

 Result = gnimmargorP C

Reverse a string using strrev

The string header has a built-in strrev() function that we can use to reverse a string. Refer to the C strrev function.

#include <stdio.h>
#include <string.h>
int main()
{
char s[100] = "Mango";
printf("%s", strrev(s));
}
ognaM

Without using a temporary variable

In the following example, we use the Bitwise XOR operator to reverse a string. This approach does not require any temporary variable to store the intermediate characters. Refer to the C Bitwise operators.

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

int main()
{
char s[100] = "New Year";
int len = strlen(s);

for(int i = 0; i < len / 2; i++)
{
s[i] ^= s[len - i- 1];
s[len - i- 1] ^= s[i];
s[i] ^= s[len - i- 1];
}
printf("%s", s);
}
raeY weN

Program to Reverse a String Using Functions

This C program is the same as above. However, we are using the C recursive Functions to reverse the original and separate the logic from the main program this time.

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

void revText(char [], int, int);
 
int main()
{
  	char Str[100], temp;
  	int i, j, len;
 
  	printf("\n Please Enter :  ");
  	gets(Str);
  	
  	len = strlen(Str);
  	revText(Str, 0, len -1);
 
  	printf("\n After = %s", Str);
  	
  	return 0;
}

void revText(char Str[], int i, int len)
{
	char temp;
	temp = Str[i];
	Str[i] = Str[len - i];
	Str[len - i] = temp;
	
  	if (i == len/2)
  	{
		return;
  	}
  	revText(Str, i + 1, len);
}
Before :   Learn Programming

After = gnimmargorP nraeL

C program to Reverse a String Using Pointers

This program to reverse a string is the same as above, but we are using Pointers to separate the logic from the main function this time. Refer to the Pointers in C.

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

char* textRe(char *Str)
{
	static int i = 0;
	static char RevStr[10];
	
	if(*Str)
	{
		textRe(Str + 1);
		RevStr[i++] = *Str;
	}
	return RevStr;
}
 
int main()
{
  	char Str[100], temp;
  	int i, j, len;
 
  	printf("\n Enter  :  ");
  	gets(Str);
 
  	printf("\n Result = %s", textRe(Str));
  	
  	return 0;
}
program to Reverse a String Using Pointers

String reverse using recursion

In the following program, we use a recursive two-pointer approach to reverse a string. Refer to the recursion in c.

#include <stdio.h>
#include <string.h>
void revStr(char *str, int s, int e)
{
if (s >= e)
{
return;
}
char temp = str[s];
str[s] = str[e];
str[e] = temp;
revStr(str, s + 1, e - 1);
}

int main()
{
char s[100] = "Hi Guys";
revStr(s, 0, strlen(s) - 1);
printf("%s", s);
}
syuG iH

Also Read

  1. C Program to Reverse a String using Recursion
  2. C Program to Reverse Order of Words in a String