Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

strrev() in C

Last Updated on January 16, 2024 by Ankit Kochar

In the realm of C programming, where efficiency and precision are paramount, understanding and utilizing various functions can significantly enhance a developer’s capabilities. One such indispensable function is strrev(), which serves the purpose of reversing a string. This article aims to unravel the intricacies of strrev() in C, exploring its syntax, applications, and potential pitfalls. Whether you’re a seasoned programmer or a novice, delving into the nuances of this function can broaden your coding repertoire and pave the way for more robust software development.

What is strrev in C?

strrev in C is an in-built function defined in the “string.h” header file which means you need to include this header file in your C program. Strrev in C helps us in reversing the string without writing our own custom logic for reversing the string.

Syntax of strrev in C

The syntax of the strrev() Function in C is as follows.

char *strrev(char *str);

This function will reverse the string by doing an in-place operation.

Parameters of strrev in C

The strrev in C takes only one parameter i.e., *str which is the pointer pointing to the string which we want to reverse.

Return Type of strrev in C

The strrev in C does not have any return type. It simply stores the reversed string in the same string.

How to use strrev in C

To reverse a String using the strrev in C, we need to follow these steps in order.

  • Step 1: First declare a null-terminated string, which you want to reverse using the strrev in C. Also, initialize the string with some value
  • Step 2: Now call the strrev function by passing the pointer to the string defined in the above step.
  • Step 3: The strrev in C reverses the string passed by doing the in-place operations.
  • Step 4: The reversed string is stored in the original string and we can use it for further processing.

Examples of strrev in C

These examples will help you in a clear understanding of the working of the strrev in C.

Example 1 of strrev in C: String with a single word
This example reverses a string containing only a single word using the strrev in C.

Code

#include <stdio.h>
#include <string.h>

int main()
{
    char str[100] = "PrepBytes";

    printf("The given string is =%s\n", str);
    strrev(str);
    printf("After reversing string is =%s", str);

    return 0;
}

Output

The given string is = PrepBytes
After reversing string is = setyBperP

Explanation:
In the above example, we have declared a character array, str of size 100, and initialized it with the “PrepBytes”. We have printed the original string on the screen i.e., “The given string is = PrepBytes”. Next, we passed the string to the strrev Function. The strrev in C then reverses the string doing the in-place. After this, we printed the reversed string “After reversing string is setyBperP” on the console.

Example 2 of strrev in C: String with multiple words
Let us now reverse a string containing multiple words using the strrev in C.

Code

#include <stdio.h>
#include <string.h>

int main()
{
    char str[100] = "PrepBytes is an Ed-Tech Company";

    // Original String 
    printf("%s\n", str);
    
    strrev(str);
    
    // Revresed String
    printf("%s", str);

    return 0;
}

Output

PrepBytes is an Ed-Tech Company
ynapmoC hceT-dE na si setyBperP

Explanation:
Here in this above code, we have declared a character array of length 100 and initialized it with a string with multiple words. Then we have reversed the String “PrepBytes is an Ed-Tech Company” by passing it to the strrev function. Then we printed the reversed string which gives output on the screen as “ynapmoC hceT-dE na si setyBperP”.

Things to Remember about strrev in C
Here are some important points that one must remember about the strrev in C.

  • This strrev() function in C modifies the original String.
  • If you want to retain the original string, you must create a copy of the string before calling the strrev in C.
  • strrev in C does not work with the constant strings.
  • This function can only reverse the ASCII characters and not the Unicode characters.
  • It is not a standard C library function and may not be available on all platforms.

Conclusion
In conclusion, the strrev() function in C stands as a powerful tool for string manipulation, offering a concise and efficient way to reverse character arrays. While its simplicity may deceive, a thorough understanding of its usage and limitations can make a substantial difference in your coding endeavors. As you navigate the vast landscape of C programming, consider incorporating strrev() judiciously, leveraging its capabilities to streamline your code and enhance the functionality of your applications.

Frequently Asked Questions (FAQs) Related to strrev() in C

Here are some Frequently Asked Questions related to “strrev in C”.

1. What is the purpose of strrev() in C?
strrev() is a function in C used to reverse the characters in a string, effectively flipping the order of the characters from the end to the beginning.

2. What is the syntax of the strrev() function?
The syntax is char strrev(char str);, where str is the input string to be reversed, and the function returns a pointer to the reversed string.

3. Can strrev() be used in C++?
No, strrev() is not a standard C++ function. It is specific to the C programming language. In C++, you can use alternative methods like std::reverse for reversing strings.

4. Does strrev() modify the original string?
Yes, strrev() modifies the input string in place. It reverses the characters within the provided string rather than creating a new reversed string.

5. Are there any limitations or drawbacks to using strrev()?
One limitation is that strrev() is not a standard function in the C standard library. Its availability is compiler-dependent. Additionally, it may not work as expected for strings with multibyte characters.

6. How can I reverse a string without using strrev()?
You can manually reverse a string by swapping the characters from the beginning to the end iteratively or by using library functions like strlen() and swapping characters based on their indices.

7. Can strrev() reverse a null-terminated string with dynamic memory allocation?
Yes, strrev() can reverse a dynamically allocated string. However, be cautious about memory management to avoid memory leaks.

8. Are there any alternatives to strrev() for reversing strings in C?
Yes, you can use loops or recursion to reverse a string manually, or employ library functions like strlen() and swap characters based on their positions.

9. Does strrev() handle wide characters (wchar_t) in C?
No, strrev() is designed for single-byte character strings. For handling wide characters, you should use functions like wcsrev() for wide character strings.

10. In which header file is strrev() declared in C?
strrev() is not a standard C library function. If available, it might be declared in non-standard headers, and its usage is compiler-dependent. It’s advisable to check your compiler’s documentation for specific details.

Leave a Reply

Your email address will not be published. Required fields are marked *