The ones who are crazy enough to think they can change the world are the ones who do.- Steve Jobs
strrev() is one of the inbuilt string function in c programming which is used to reverse a given string.
The following diagram clearly illustrate the working principle of strrev() inbuilt string function in C.
In the above diagram strrev() takes one parameter which is a string . strrev() will reverse the given string.
strrev(str1);
Let us work through strrev() function. In the following program we will reverse the string using strrev() inbuilt string function.
#include <stdio.h> #include<string.h> int main() { char str1[20] = "AMBULANCE"; printf(" %s ", strrev(str1)); return 0; }
ECNALUBMA
The above program defines the function strrev(), which is used to reverse the characters in a given string.
Let us reverse the string without using inbuilt string function strrev().
#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
The above program looks verbose but yields the same result.
We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.
© Copyright 2019