I need to build a function which gets two strings checks if their chars are alpha type
in ascending order.
If they both are then it needs to merge them in a third d string.
I cant call external functions.
The whole process must be in one function
and no iterative loops
and only in a recursive way.
I tried to build this function.
i think it works
but just after i input
abc in the first string and def into the second one
it does nothing and says that my program the EXE file
encountered a problem
and needs to be closed
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;
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("input is invalid.\n");
}
return 0;
}
int merge_strings(char str1[], int index1,char str2[],int index2, char result[], int index3)
{
int res,index;
int res2;
//start checking apha chars for string 1
if ((((str1[index1]>= 65) &&(str1[index1]<= 90))||((str1[index1]>= 97) &&(str1[index1]<= 122)))&&(index==254))
{
return 1;
}
if (((str1[index1]>= 65) &&(str1[index1]<= 90))||((str1[index1]>= 97) &&(str1[index1]<= 122)))
{
res=merge_strings(str1, index++,str2,index2, result, index3);
}
else
{
return 0;
}
//end checking apha chars for string 1
//start checking apha chars for string 2
if ((((str1[index1]>= 65) &&(str1[index1]<= 90))||((str1[index1]>= 97) &&(str1[index1]<= 122)))&&(index==254))
{
return 1;
}
if (((str1[index1]>= 65) &&(str1[index1]<= 90))||((str1[index1]>= 97) &&(str1[index1]<= 122)))
{
res2=merge_strings(str1, index,str2,index2++, result, index3);
}
else
{
return 0;
}
//end checking apha chars for string 2
if ((res==1)&&(res2==1))
{
result[index3]=str1[index];
if(index<255)
{
res=merge_strings(str1, index++,str2,index2, result, index3++);
}
result[index3]=str2[index2];
if(index2<255)
{
res=merge_strings(str1, index,str2,index2++, result, index3++);
}
}
return 1;
}