recursion. I tested other online solutions and they seem to be almost identical to mine. The code should cancel the line (in its position), but it is not. For example, when you type st2 = "abcdefg", the output is an empty string. I was expecting st2 = "gfedcba". What am I missing?
#include <stdio.h>
#include <string.h>
void recurse_reverse(char s[], int sz)
{
int i=0,j = sz -1;
if(i<j)
{
swap(&s[i],&s[j]);
recurse_reverse(s+1, sz-2);
}
}
void swap( char* s1, char *s2)
{
char tmp;
tmp = *s1;
*s1 = *s2;
*s2 = tmp;
}
int main(void)
{
char st1[9] = "abcdefg", st2[9];
strcpy(st2,st1);
recurse_reverse(st2,9);
printf("s1 = %s\ns2 = %s",st1,st2);
printf("\n" );
return 0;
}
source
share