how to merge two arrays recursevly..
i tried to pass the end index of the first
to the start index of the second one.
but i get an endless loop
why??
Code:
#include <stdio.h>
int merge_strings(char str1[], int index1,char str2[],int index2, char result[], int index3);
int main() {
char input[255];
char input2[255];
char result[510];
int index,flag,ch,lnd;
printf("enter the first string \n");
for (index = 0; index < 254 && (ch = getchar()) != '\n' && ch >=0; ++index)
{
input[index] = ch;
}
input[index] = '\0';
printf("enter the second string \n");
for (index = 0; index < 254 && (ch = getchar()) != '\n' && ch >=0; ++index)
{
input2[index] = ch;
}
input2[index] = '\0';
flag=merge_strings(input, 0,input2,0, result, 0);
if (flag)
{
printf("%s is valid.\n",result);
}
else
{
printf("%s is invalid.\n",result);
}
return 0;
}
int merge_strings(char str1[], int index1,char str2[],int index2, char result[], int index3)
{
int check,check2,check3,check4;
if(str1[index1]=='\0')
{
return index1;
}
else
{
result[index1]=str1[index1];
check=merge_strings(str1,index1+1,str2,index2,result,index3);
}
//printf("index1 is:%d str1[index1] is : %c\n ",index1,str1[index1]);
if(str2[index2]=='\0')
{
return 1;
}
else
{
result[index2]=str2[index2];
check=merge_strings(str1,index1,str2,check+1,result,index3);
}
return 1;
}//end function