C strrev() Function

The strrev() function in C is used to reverse a string. It is not a standard function in the C library, but it can be implemented easily. The function reverses the characters in a string in-place.

Syntax of strrev()

The syntax of the strrev() function is as follows:

Syntax:

                        
char *strrev(char *str);
                        
                    

The function takes one argument:

The function returns a pointer to the reversed string.

Example of strrev() Function

Here is an example that demonstrates how the strrev() function works. Since strrev() is not a part of the standard C library, we will implement it manually.

Example:

                        
#include <stdio.h>>
#include <string.h>>
        
// Function to reverse a string
char* strrev(char* str) {
    int len = strlen(str);
    int i, j;
    char temp;
                
// Reversing the string
    for (i = 0, j = len - 1; i < j; i++, j--) {
        temp = str[i];
            str[i] = str[j];
            str[j] = temp;
    }
    return str;
    }
        
int main() 
{
    char str[] = "Hello, World!";
                
    printf("Original String: %s\n", str);
                
    // Reversing the string
    strrev(str);
                
    printf("Reversed String: %s\n", str);
                
    return 0;
}
                        
                    

Output:

Original String: Hello, World!
Reversed String: !dlroW ,olleH

Explanation of the Example

In this example:

Important Notes