C

Reverse a String In C Using Strrev Function

In this tutorial, we are going to see how to reverse a string in C using strrev function. For example, if a user enters the string “StackHowTo”, it will be “oTwoHkcatS” when reversed. A string that stays the same when reversed is a string named Palindrome.

There are four ways to reverse a string in C, by using for loop, pointers, recursion, or by strrev function.
 

Reverse a String In C Using Strrev Function
#include <stdio.h>  
#include <string.h>  

int main()  
{  
	char str[50];
	printf (" \nEnter a string: ");  
	scanf ("%s", str);  
			  
	// use strrev function to reverse a string  
	printf (" \nString after reversing it: %s ", strrev(str));  
	return 0;  
}

Output:

 Enter a string :  StackHowTo
 String after reversing it: oTwoHkcatS

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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