The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

strrev() In C

Purpose of strrev()

strrev() is one of the inbuilt string function in c programming which is used to reverse a given string.

How strrev() Works

The following diagram clearly illustrate the working principle of strrev() inbuilt string function in C.

strrev() in c

In the above diagram strrev() takes one parameter which is a string . strrev() will reverse the given string.

Syntax - strrev()

  • strrev() accept single parameter.
  • Parameter must be a string.
  • To use strrev() inbuilt string function in C, we need to declare #include<string.h> header file.
Syntax
strrev(str1);

C Program - strrev()

Let us work through strrev() function. In the following program we will reverse the string using strrev() inbuilt string function.

c-using-strrev.c
#include <stdio.h>
#include<string.h>
int main()
{
char str1[20] = "AMBULANCE";
printf(" %s ", strrev(str1));
return 0;
}
ECNALUBMA

Note:

The above program defines the function strrev(), which is used to reverse the characters in a given string.

C Program - Without strrev()

Let us reverse the string without using inbuilt string function strrev().

c-without-strrev.c
#include <stdio.h>
#include<string.h>
int main()
{
char str[20] = "AMBULANCE";
char str2[20];
int i, j, k;
for(i=0; str[i]!='\0'; i++){
//finding length of a string
}
k=i-1;
for(j=0;j<i;j++)
{
str2[j]=str[k];
k--;
}
for(i=0; str[i]!='\0'; i++){
printf("%c",str2[i]);
}
return 0;
}
ECNALUBMA

Note:

The above program looks verbose but yields the same result.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report