I wanna make a recursive function which invert a string so i tried this code but it doesn't work correctly . I try to explain my code :
-I made a function which called 'concat' , this function took tow strings as arguments and return a string after the concatunation
-In the 'inv' function which is concerned to invert a string taken as an argument , i tried to concatinate in each loop the i-th caracter whith the previous .
#include <stdio.h>
#include <string.h>
char *concat(char *ch1, char *ch2)
{
char *ch3 = malloc((strlen(ch1) + strlen(ch2) + 1) * sizeof(char));
strcat(ch1, ch2);
strcpy(ch3, ch1);
return (ch3);
}
char *inv(char *ch1, int i, char *ch2)
{
if (i == 0)
{
strncat(ch2, ch1, 1);
return ch2;
}
if (i > 0)
{
return concat(&ch1[i], inv(ch1, i - 1, ch2));
}
}
int main(int argc, char *argv[])
{
char ch1[10];
char ch2[10];
strcpy(ch2, "");
scanf("%s", ch1);
printf("%s", inv(ch1, strlen(ch1) - 1, ch2));
return 0;
}