The task is to pass reverse string of a given string and print it in the function.
When I was trying to do that the code is printing the actual string instead of reverse string.
#include<stdio.h>
#include<string.h>
void function(char*);
int main()
{
char x[]="Hello";
printf("\nPassing: %s\n",x);
function(x);
printf("\nPassing: %s\n",strrev(x));
function(strrev(x));
}
void function(char mainstr[])
{
printf(" >Recieved mainstr=%s",mainstr);
}
Output of the above code is:
Passing: Hello
>Recieved mainstr=Hello
Passing: olleH
>Recieved mainstr=Hello
What is the mistake in this code. Can anyone please elaborate?, Thank you.
You call strrev
twice. The second call undoes the effects of the first call. If you want to keep the string reversed, don't call strrev
again as reversing the same string twice puts it back the way it was.