I have this problem with an assignment. Im supposed to reverse a string recursively into another empty string. The thing is that the function modifies the source string which it is not supposed to do, just to copy the string backwards to the destination string. I don't understand why this is happening...
#include <stdio.h>
#include <string.h>
void
invert(const char *src, char dest[])
{
if(*src=='\0')
return;
else
{
invert(src+1, dest);
dest[strlen(src)-1]=*src;
}
}
int main(int argc, const char * argv[])
{
char dest[]="";
char src[]="";
printf("write a word: \n");
scanf("%s", src);
invert(src, dest);
dest[strlen(src)]='\0';
printf("the inversion of the word is: %s\n", dest);
return 0;
}
For example: writing Ulysses => sessesU and writing Jones => seesJ\377