strrev in c

What is strrev in c?

strrev in c is used to reverse a given string. It is declared in string.h header file.

It is a built-in function in c.

The strrev function terminates when reversing process is over.

Prototype

char *strrev(char * strin);

Parameters for strrev in c

This function takes string which is needed to be reversed.

Return value of strrev in c

This function returns a reversed string.

strrev example programs

Example 1

#include<stdio.h>
#include<string.h>
int main()
{
   char str[30];
   printf("\n\n\tEnter a word to reverse.\n\t");
   scanf("%s",str);
   printf("\n\tYou have entered: %s \n",str);
   printf("\tReversed string is :%s \n",strrev(str));
   return 0;
} 
strrev example in c 1
strrev example in c 1
        Enter a word to reverse.
	scholar_soul

	You have entered: scholar_soul 


	Reverse string is:
	luos_ralohcs

While compiling the program I have encountered following error.

example 1.c: In function ‘main’:
example 1.c:8:4: warning: implicit declaration of function ‘strrev’; did you mean ‘strsep’? [-Wimplicit-function-declaration]
    8 |    strrev(str);
      |    ^~~~~~
      |    strsep
/usr/bin/ld: /tmp/ccwSIf1D.o: in function `main':
example 1.c:(.text+0x51): undefined reference to `strrev'
collect2: error: ld returned 1 exit status

Its because I was using gcc compiler. In gcc compiler strrev is not found.

Code explanation

Here we include string.h header inorder to execute strrev() function.

Here we declare an array st and we initialize it with value schoalrsoul . We print the statement before string reverse operation is computed to the console. Another print statement is executed after the use of strrev operation and a reversed string is printed onto a console.

Example 2

#include<stdio.h>
#include<string.h>
int main()
{
   char  str[40]="229092";
   printf("The string is :%s \n",str);
   printf("Reversed string is :%s \n",strrev(str));
   return 0;
}
strrev example in c 2
strrev example in c 2
	The string is :229092 
	Reversed string is : 
	290922

Code explanation

Here we are declaring a string “229092” then printing it using printf.

Then we are printing a reversed string using return value of strrev as parameter to printf .


Difference between strlen and strrev in c

strlen in c is used to calculate the length of a string. strrev in c is used to reverse a given string.

strlen returns total length of a string. strrev returns a reversed string.

strlen terminates when it encounters null character. strrev terminates when process ends.

strlen returns integer value. strrev returns a reversed string.

General forms are as follows

int  strlen(const char*strng);

char *strrev(char * strin);
strlen in cstrrev in c
Used to calculate the length of a string.Used to reverse a given string.
int strlen(const char*strng);char *strrev(char * strin);
Returns integer value.Returns a reversed string.

Difference between strrev and strcpy in c

strrev in c is used to reverse a given string. strcpy is used to copy the source string to destination string.

strrev returns a reversed string. strcpy returns pointer to the destination string.

strcpy takes two arguments. strrev takes one argument.

strcpy function takes two pointers. strrev takes one pointer to char array.

General forms are as follows


char *strrev(char * strin);


char *strcpy(char *destn,const char*src);
strrev in cstrcpy in c
Used to reverse a given string.Used to copy the source string to destination string.
char *strrev(char * strin);char *strcpy(char *destn,const char*src);
Returns a reversed string.Returns pointer to the destination string.
Takes one argument.Takes two arguments.

Difference between strrev and strchr in c

strchr is used to search for the first occurrence of a character in the string pointed to by string argument. strrev in c is used to reverse a given string.

strchr returns a pointer to the first occurrence of a character in a string otherwise NULL if not found. strrev returns a reversed string.

strchr takes two arguments. strrev takes one argument.

General forms are as follows



char *strrev(char * strin);

char *strchr(const char*string,int a);
strrev in cstrchr in c
Used to reverse a given string.Used to search for the first occurrence of a character in the string pointed to by string argument.
char *strrev(char * strin);char *strchr(const char*string,int a);
Returns a reversed string.Returns a pointer to the first occurrence of character in a string otherwise NULL if not found.
Takes one argument.Takes two arguments.

Difference between strrev and strcmp in c

strcmp function is used to compare two strings character by character. strrev in c is used to reverse a given string.

strcmp function returns 1.returns value<0 if st1 is less than st2. 2.it returns value>0 if st2 is less than st1. 3. it returns value=0 if boyh are equal. strrev returns a reversed string.

strcmp takes two arguments. strrev takes one argument.

strcmp returns one of the three integer value(negative,positive,zero). strrev returns reversed string.

General forms are as follows


int strcmp(const char*str1,const char*str2);

char *strrev(char * strin);
strrev in c strcmp in c
Used to reverse a given string.Used to compare two strings character by character.
char *strrev(char * strin);int strcmp(const char*st1,const char*st2);
Returns a reversed string.Returns 1.returns value<0 if st1 is less than st2. 2.it returns value>0 if st2 is less than st1. 3. it returns value=0 if boyh are equal.
Takes one argument.Takes two arguments.

Difference between strcat and strrev in c

strcat is used to append source string to destination string. strrev in c is used to reverse a given string.

strcat returns a pointer to the final string. strrev returns a reversed string.

strcat takes two arguments. strrev takes one argument.

General forms are as follows


char *strcat(char *dest, const char *src);

char *strrev(char * strin);
strrev in cstrcat in c
Used to reverse a given string.Used to append source string to destination string.
char *strrev(char * strin);char *strcat(char *dest, const char *src);
Takes one argument.Takes two arguments.

People also ask for

What is the use of strrev function in c?

It is used to obtain reversed string. For example “abcd” is reversed as “dcba” using builtin function strrev in c.

What is palindrome string in c?

Palindrome means it reads backward and forwards string or a number as same. For example “121” is the same when u read it in a forward and reversed manner. You can use strrev to check the given string is palindrome or not.

Why Strrev is not working in C?

It’s probably because you didn’t include string.h header file. It’s because your c compiler doesn’t have the declaration for strrev in the string header file. strrev is only available in ANSI C compilers.


People also read