C program to reverse a string is a program to reverse the given string using string function in c.
In this tutorial we are going to learn new one of the important and simple program which is program to reverse a string.
Before learning reverse string program in c we first see string in c.
String in c
String in c is a collection of character. And it ends with the null character.
In c string has different string function such as strcmp, strrev, strlen etc.
String reverse in c
strrev in c : To find the reverse of the string in c the string class provided one function strrev() using this function we can find reverse of the string.
For using this function we need to include string.h header file.
Syntax for strrev in c
strrev function in c
This function reverse given string.
strrev example in c :-
The reverse of string is lacinhcet.
Logic to find string reverse in c
First we declare string.h header file. Next week take input string from user. Now using the strrev() find reverse of string. Print the reverse of the string.
Algorithm to string reverse in c
2. Declare variable
3. Take a string input from user
4. Find reverse of the string using string function
5. Print the revered string
C program to reverse a string
printf("Enter a string to get reverse of string\n");
printf("The reverse of the given string is---> %s\n",strrev(str));
1. Write a header files stdio.h and string.h as we are using string function.
2. Next write the main function from where the execution of program starts.
3. Declare the variables
str => to store the string
4. Next take a input string from user.
printf(“Enter a string to get reverse of string\n”);
We use a gets() to take a input string from user.
Difference between scanf and gets is scanf don’t accept space in string but gets accepts space in string.
5. Example :-
str = technical
6. Next we find reverse the string using string function
strrev(str) => strrev reverse the string
7. Next we print the reversed string
printf(“The reverse of the given string is = %s\n”,strrev(str));
=> The reverse of the given string is = lacinhcet