Understanding Strrev() Function in C

Understanding Strrev() Function in C

3 mins read286 Views Comment
Updated on Oct 3, 2023 11:53 IST

We have curated this article to provide you information about strrev() function in C programming.

2023_03_Copy-of-Feature-Image-Templates-3.jpg

On occasion, we may need to reverse a string within our program. We can accomplish this in the C programming language using the strrev() function. In this tutorial, we will discuss this non-standard function in C. We will be covering the following sections:

So, without further ado, let’s get started!

Must Explore – Programming Courses

Introduction to strrev() Function in C

In C, the strrev() function reverses a given string. It is a built-in function that is defined in the string.h header file. The function takes a string as input and modifies the original string by reversing it.

Here is the syntax for the strrev() function:

 
char* strrev(char* str);
Copy code

Parameters of strrev()

There is only one parameter to strrev(), which is a pointer to a character array which needs to be reversible.

Return Value of strrev()

Function strrev() returns the same string in reverse order.

 It’s important to note that the strrev() function modifies the original string in place, which means the change in original string. Therefore, you should make a copy of the original string if you need to preserve its original value.

Also, please keep in mind that the strrev() function is non-standard and may not be available in all compilers. Instead, we can create our own implementation of a string reversal function.

Recommended online courses

Best-suited C / C++ courses for you

Learn C / C++ with these high-rated online courses

– / –
40 hours
4 K
2 months
– / –
6 months
– / –
1 month
3.5 K
3 months
15 K
2 months
– / –
1 month
– / –
2 months
– / –
50 hours
– / –
4 months

C Examples of strrev() Function 

Example 1: Checking if a string is a palindrome

 
#include <stdio.h>
#include <string.h>
int main() {
char str[100], rev[100];
printf("Enter a string: ");
gets(str);
strcpy(rev, str);
strrev(rev);
if (strcmp(str, rev) == 0)
printf("%s is a palindrome", str);
else
printf("%s is not a palindrome", str);
return 0;
}
Copy code

Output:

Enter a string: radar
radar is a palindrome

In this example the user is requested to enter a string. The strcpy() function [insert link] is used to copy the entered string into another string called rev. Then, we can use the strrev() function to reverse the copied string. Finally, the strcmp() function [insert link] compares the original and reversed strings. If the strings are equal, the input string is a palindrome, and the program will output “is a palindrome”. If not, the program will output “is not a palindrome”.

Example 2: Taking User Input and Reversing it

 
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("Enter a string: ");
gets(str);
strrev(str);
printf("Reversed string: %s", str);
return 0;
}
Copy code

Output:

Enter a string: Hello World
Reversed string: dlroW olleH

In this example the user is requested to enter a string. We can use the gets() function to read the entered string from the user, and then the strrev() function reverses the string. Finally, the reversed string is printed on the screen.

Example 3: Reversing words in a sentence

 
#include <stdio.h>
#include <string.h>
int main() {
char sentence[100];
printf("Enter a sentence: ");
gets(sentence);
char *token = strtok(sentence, " ");
while(token != NULL) {
strrev(token);
printf("%s ", token);
token = strtok(NULL, " ");
}
return 0;
}
Copy code

Output:

Enter a sentence: Welcome to Naukri Learning
gninraeL irkuaN ot emocleW

In this example the user is requested to enter a string. The strtok() function [insert link] tokenises the sentence by space. The strrev() function then reverses each token (word) in the sentence. Finally, the reversed sentence is printed on the screen.

Example 4: Reversing a substring within a string

 
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("Enter a string: ");
gets(str);
int start, end;
printf("Enter the start and end index of the substring to reverse: ");
scanf("%d %d", &start, &end);
int len = strlen(str);
char temp[len];
for(int i = 0; i < len; i++) {
if(i >= start && i <= end) {
temp[i-start] = str[i];
} else {
temp[i] = str[i];
}
}
strrev(temp);
for(int i = 0; i < len; i++) {
if(i >= start && i <= end) {
str[i] = temp[i-start];
}
}
printf("Reversed substring: %s", str);
return 0;
}
Copy code

Output:

Enter a string: Hello World
Enter the start and end index of the substring to reverse: 3 7
Reversed substring: Helld oWorl

In this example the user is requested to enter a string and the start and end index of the substring to reverse. We can first copy the string into a temporary array, excluding the substring to be reversed. We can use the strrev() function to reverse the substring within the temporary array. At the end, the reversal substring is printed on the screen at the correct position in the original string.

C programming Keywords: A list and Explanation of Usage
What is Palindrome in C Programming?
Variables in C Programming: Types, Scope, and Lifetime

Endnotes

In this article, we have seen various examples of using the strrev() function, including reversing a string, checking if a string is a palindrome, and reversing words in a sentence. In conclusion, the strrev() function can save time and effort when reversing a string in C programming language. Still, it is important to know its limitations and potential compatibility issues with certain compilers. 

I hope this article was helpful for you in understanding that. If you want to learn more about C programming and solidify your basics, you can explore our articles on C.

Contributed By: Prerna Singh

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio