#include<stdio.h>
#include<string.h>
char *reverse(char str[])
{
int temp;
char val[100]="";
for(int j=0;j<(strlen(str)/2);j++)
{
temp=str[j];
str[j]=str[strlen(str)-j-1];
str[strlen(str)-j-1]=temp;
}
strcat(val,str);
return val;
}
int main()
{
char str[20];
printf("Enter a string:\n");
scanf("%9s",str);
printf("The Reverse is: \n%s",reverse(str));
return 0;
}
Here is the output.
Enter a string:
Banana
The Reverse is:
(null)
[Process completed - press Enter]
This program had to take string input from a user and store the reverse of that string into a variable using a function. But when I print it, it shows '(null)'. Explanations and suggestions regarding this are appreciated.