What is strrev() Function in C language?


An array of characters is called a string.

Declaration

The syntax for declaring an array is as follows −

char stringname [size];

For example − char string[50]; string of length 50 characters

Initialization

  • Using single character constant −
char string[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’}
  • Using string constants −
char string[10] = "Hello":;

Accessing − There is a control string "%s" used for accessing the string till it encounters ‘\0’.

The strrev( ) Function

  • This function is used for reversing a string.
  • The reversed string is stored in the same string.

Syntax

The syntax for strrev() function is as follows −

strrev (string)

Example

The following program shows the usage of strrev() function.

#include<stdio.h>
main ( ){
   char a[50] ;
   clrscr( );
   printf ("enter a string");
   gets (a);
   strrev (a);
   printf("reversed string = %s",a)
   getch ( );
}

Output

When the above program is executed, it produces the following result −

enter a string Hello
Reverse string = olleH

Example 2

Let’s see another example to know more about string reverse.

The C program to reverse a string using strrev library function is given below −

#include<stdio.h>
#include<string.h>
void main(){
   char string[25];
   printf("Enter String to be reversed : ");
   gets(string);
   printf("String before strrev(): %s
",string);    strrev(string);    printf("The string after strrev(): ");    puts(string); }

Output

When the above program is executed, it produces the following result −

Enter String to be reversed : Tutorials Point
String before strrev(): Tutorials Point
The string after strrev(): tnioP slairotuT

Updated on: 19-Mar-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements