C Program to Reverse a String using Recursion

Write a C program to reverse a string using recursion or recursive functions. In this c string reverse example, the stringReverse function accepts the string pointer and reverses the string by calling the function recursively.

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

void stringReverse(char *Str)
{	
	if(*Str)
	{
		stringReverse(Str + 1);
		printf("%c", *Str);
	}
}
 
int main()
{
  	char Str[100];
 
  	printf("Please Enter any String to Reverse =  ");
  	fgets(Str, sizeof Str, stdin);
 
  	printf("\nString after Reversing = ");
	stringReverse(Str);
	printf("\n");
}
C Program to Reverse a String using Recursion

This c program uses the temp variable, recursive functions, and pointers to reverse a given string.

#include <stdio.h>
#include <string.h>
void stringReverse(char *Str, int i, int len)
{
	char temp;	
  	if (i >= len)
  	{
		return;
  	}
	  
	temp = *(Str + i);
	*(Str + i) = *(Str + len);
	*(Str + len) = temp;
  	stringReverse(Str, ++i, --len);
}
 
int main()
{
  	char Str[100];
 
  	printf("\nPlease Enter any String to Reverse =  ");
  	fgets(Str, sizeof Str, stdin);

  	stringReverse(Str, 0, strlen(Str) -1);
 
  	printf("\nString after Reversing = %s\n", Str);
  	
  	return 0;
}
Please Enter any String to Reverse =  hello c programmers

String after Reversing = 
sremmargorp c olleh

It is another c example to reverse a string using recursion.

#include <stdio.h>
void stringReverse()
{	
	char ch;
	scanf("%c", &ch);

	if(ch != '\n')
	{
		stringReverse();
		printf("%c", ch);
	}
}

int main()
{ 
  	printf("Please Enter any String to Reverse =  ");
  	stringReverse();
	printf("\n");
}
Please Enter any String to Reverse =  c examples
selpmaxe c