strrev() in C
×


strrev() in C

58

 Imagine you have a word written forward, like "hello".

 Now, if you want to flip it backward to say "olleh", you'd use strrev().

 Essentially, it helps reverse a string, changing its order from the last character to the first.

 strrev() is the function responsible for reversing a string.

 It takes a string str as input and reverses its characters in place.

 After applying strrev(), the original string str is transformed into its reverse form.

Example:

// Program for strrev function in c
#include<stdio.h> #include<string.h> // Function to reverse a string void strrev(char *str) { int length = strlen(str); // Find the length of the string int start = 0; // Start index of the string int end = length - 1; // End index of the string // Swap characters from start and end of the string while (start < end) { // Swap characters char temp = str[start]; str[start] = str[end]; str[end] = temp; // Move start index forward and end index backward start++; end--; } } int main() { char str[] = "hello"; // Input string printf("Original string: %s\n", str); // Print original string strrev(str); // Reverse the string printf("Reversed string: %s\n", str); // Print reversed string return 0; }

Output:

Original string: hello
Reversed string: olleh

Advantage of strrev() in C 

Simple Reversal: strrev() provides an easy way to reverse a string without needing complex logic.

 Efficiency: It quickly rearranges the characters, saving time and effort.

 Versatility: You can use strrev() on any string, regardless of its length or content.

 Standard Function: strrev() is a standard function in C, widely used and understood by programmers.



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments