Newsflash for you: stack overflow!
You do nothing but recursively call your function over and over again.
i cant use my own external functions either
What is your exit condition for merging the strings?
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
i check index<255
if its larger then its not going further.
define your terminal condition when you reach the end of the string to prevent stack overflow.
My opinion is that you need to redo this whole code / function. It's way too complicated and has ands and ors put out without any ( and ), even. And what does all those indexes have to do with things?
Why don't you start by writing it all in pseudo code?
i changed the first if in the merging part to
index1<255
but its still gives me the same result
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=0; 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(index1<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; }
Last edited by transgalactic2; 01-08-2009 at 03:39 PM.
I'm not talking about EXTERNAL functions - or do you mean to say that the assignment clearly says that this has to be done in ONE FUNCTION? If so, I'd say it's worse than learning to tie your shoe-laces with one hand - you are now having to do it with one hand and your thumb and forefinger tied together with a sticking plaster. That is, it is utter lunacy to do this in ONE FUNCTION.
Edit: I would double-check with your teacher (or whatever) that you are actually supposed to do all of this in ONE FUNCTION. And if that is the case, go look for another school and ask for your money back [assuming you paid for the course], as it is not at ALL a good course.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
i changed the index=0 to index
now its giving me the exe error again
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(index1<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; }
index is not set to any value at this point...Code: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))
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.