In C, the strrev()
function is not a standard library function. However, you can easily write your own function to reverse a string. Here’s an example of how you can implement a reverse string function in C:
#include <stdio.h> #include <string.h> void reverseString(char* str) { int length = strlen(str); int i, j; char temp; for (i = 0, j = length - 1; i < j; i++, j--) { temp = str[i]; str[i] = str[j]; str[j] = temp; } } int main() { char str[] = "Hello, World!"; printf("Original string: %s\n", str); reverseString(str); printf("Reversed string: %s\n", str); return 0; }
In this example, we define the reverseString()
function that takes a character array (string) as an argument and reverses it in place. It uses two indices, i
and j
, starting from the beginning and end of the string, respectively. We swap the characters at these indices and move towards the center of the string until we reach the middle. Finally, we print the original and reversed strings in the main()
function.
Output:
Original string: Hello, World! Reversed string: !dlroW ,olleH
Note that the strrev()
function was available in some older versions of C, but it is not a standard function and is not recommended for use due to security concerns. Writing your own reverse string function allows you to have more control over the process and avoid potential issues.