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 March 30, 2023 by Prepbytes

In C programming language, a string is a data structure that is basically an array of characters that ends with a special null character, ‘\0’. C Language provides many functions which help the programmer in manipulating the strings. One such function is strrev in C which is used for reversing the string. Let us learn about this function in detail, its syntax, it’s working, and examples.

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
The strrev function in C programming is a built-in function, which is used to reverse a string. It takes a null-terminated string as input and returns a pointer to the reversed string. The function reverses the characters of the string in place, i.e., it modifies the original string. The strrev function is defined in the string.h header file. It is important to keep in mind that the strrev function modifies the original string and does not work with constant strings

Frequently Asked Questions (FAQs)

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

Ques 1. What header file is required for strrev?
Ans. The header file "string.h" is required for using the strrev function.

Ques 2. Can strrev be used to reverse a character array?
Ans. Yes, strrev can be used to reverse a character array as well.

Ques 3. Does strrev modify the original string?
Ans. Yes, strrev modifies the original string. It does not create a new string.

Ques 4. What happens if NULL is passed as an argument to strrev?
Ans. If NULL is passed as an argument to strrev, it will result in a segmentation fault.

Ques 5. What is the time complexity of strrev in C?
Ans. The time complexity of strrev is O(n), where n is the length of the string.

Leave a Reply

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