Reversing a string in C can be done by strrev() function in a single line. This function simply reverses the given string in a program.
The strrev() function is defined in the header file string.h
.
It takes a single argument that is the name of the string and is written as follows.
strrev(string_name);
C Program for strrev() function
1 2 3 4 5 6 7 8 9 10 11 12 | #include <stdio.h> #include <string.h> int main() { char str[] = "simple2code"; printf("Original String: %s", str); printf("Reversed str String: %s", strrev(str)); return 0; } |
Output:
Original String: simple2code
Reversed str String: edoc2elpmis
Don’t be alarmed if it does not run on your compiler. It is just that some function like strrev(), which are only available in ANSI C (Turbo C/C++) and are not available in the standard C-GCC compiler.