C Program to Reverse a String

C Program to Reverse a String

C program to reverse a string; Through this tutorial, we will learn how to reverse a string in a c program using for loop, function, and pointer.

Programs and Algorithm to Reverse a String in C

  • C program to Reverse a String using For Loop
  • C program to Reverse a String using Function
  • C program to Reverse a String using Pointer

Algorithm to Reverse a String

Use the following algorithm to write a program to reverse a string; as follows:

  • Start
  • Declare all the variables ( integer and character type )
  • Enter the string to be reversed
  • Find out the length of string
  • Swap the position of array element using loop
  • Store the swapped position
  • Print the reversed string as console output
  • Stop

C program to Reverse a String using For Loop

#include <stdio.h>
#include <string.h>
 
int main()
{
  	char Str[100], RevStr[100];
  	int i, j, len;
 
  	printf("\n Please Enter any String :  ");
  	gets(Str);
  	
  	j = 0;
  	len = strlen(Str);
 
  	for (i = len - 1; i >= 0; i--)
  	{
  		RevStr[j++] = Str[i];
  	}
  	RevStr[i] = '
#include <stdio.h>
#include <string.h>
int main()
{
char Str[100], RevStr[100];
int i, j, len;
printf("\n Please Enter any String :  ");
gets(Str);
j = 0;
len = strlen(Str);
for (i = len - 1; i >= 0; i--)
{
RevStr[j++] = Str[i];
}
RevStr[i] = '\0';
printf("\n String after Reversing = %s", RevStr);
return 0;
}
'; printf("\n String after Reversing = %s", RevStr); return 0; }

The output of the above c program; as follows:

Please Enter any String :  hello
String after Reversing = olleh

C program to Reverse a String using Function

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

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

void reverse_String(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;
  	}
  	reverse_String(Str, i + 1, len);
}

The output of the above c program; as follows:

Please Enter string :  function
After Reversing = noitcnuf

C program to Reverse a String using Pointer

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

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

The output of the above c program; as follows:

Please Enter String :  wow
Result = wow

Recommended C Programs

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *