Reverse the String Using Library Function
In C, we have a library function defined inside <string.h> that can be used to reverse a string. The strrev() function provides the simplest method to reverse the string.
Syntax
char* strrev(char* str);
where, str is the string to be reversed.
Note: The strrev() function is not a part of the standard C language, so it might not be present in every compiler.
Implementation
C
// C program to reverse a string using strrev() #include <stdio.h> #include <string.h> int main() { char str[] = "string" ; printf ( "Original String: %s\n" , str); // reversing string printf ( "Reversed String: %s" , strrev(str)); return 0; } |
Output
Original String: string
Reversed String: gnirts
Reverse String in C
Reversing a string in C is a fundamental operation that involves rearranging the characters in a string so that the last character becomes the first, the second-to-last character becomes the second, and so on.
For example,
Original String:
"string"
Reversed String:
"gnirts"
In this article, we will discuss different ways to reverse a string in C with code examples.