i changed it
like itCbitC said.
i dont get the exe error but i dont have any output either
??
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, index1++,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, index1,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, index1++,str2,index2, result, index3++); } result[index3]=str2[index2]; if(index2<255) { res=merge_strings(str1, index1,str2,index2++, result, index3++); } } return 1; }
So, now the question is: Where does index change? And what do you actually use index for in your function?
--
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 removed index. and i changed the code to the proper "index" from the signature
it doesnt give me an output at all
??
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; int res2; //start checking apha chars for string 1 if ((((str1[index1]>= 65) &&(str1[index1]<= 90))||((str1[index1]>= 97) &&(str1[index1]<= 122)))&&(index1==254)) { return 1; } if (((str1[index1]>= 65) &&(str1[index1]<= 90))||((str1[index1]>= 97) &&(str1[index1]<= 122))) { res=merge_strings(str1, index1++,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)))&&(index1==254)) { return 1; } if (((str1[index1]>= 65) &&(str1[index1]<= 90))||((str1[index1]>= 97) &&(str1[index1]<= 122))) { res2=merge_strings(str1, index1,str2,index2++, result, index3); } else { return 0; } //end checking apha chars for string 2 if ((res==1)&&(res2==1)) { result[index3]=str1[index1]; if(index1<255) { res=merge_strings(str1, index1++,str2,index2, result, index3++); } result[index3]=str2[index2]; if(index2<255) { res=merge_strings(str1, index1,str2,index2++, result, index3++); } } return 1; }
Last edited by transgalactic2; 01-08-2009 at 04:25 PM.
So, AGAIN: What is your exit condition. What do you have to do to get there?
--
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.
this is my exit condition
it returns 1 when all the chars are ok.Code:if ((((str1[index1]>= 65) &&(str1[index1]<= 90))||((str1[index1]>= 97) &&(str1[index1]<= 122)))&&(index1==254)) { return 1; }
in the merging part when the index reaches 255 it doesnt go to the if again
thus stopping the recursion and returning 1
Doesn't merging mean that you pick characters from one or another string so that the resulting string would also be in ascending order? Otherwise what is the purpose of checking whether the input strings are in ascending order?
Or is this task simply a collection of pretty arbitrary requirements (I don't see any relevance in the A...Z check) in hope that no other teacher has ever asked anybody to write such a bizarre program (and you can't search for the code on the internet)?
This exit condition would appear to be always false (the last character in the max-length string shouldn't be anything other than '\0'), or you have already passed the end of the string and the result of that is undefined...this is my exit condition
Also, could you for a start write this using a simple loop. It is usually trivial to turn a single loop into a recursion.
Last edited by anon; 01-08-2009 at 04:39 PM.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
ok i understand that str2 is written over str1 places
i dont know how to fix it
So you want to copy first string and append the other? Can you do this with a loop? The other things like testing for order and character values is just a distraction that has little to do with the recursion (except perhaps you have to reserve the return value for signalling error).
Also, does it have to be all just one function, besides main?
Otherwise it might go something like this:
Code:int recursive_copy_with_weird_extra_conditions(char** result, const char* input); int funny_merge(const char* first, const char* second, char* result) { return recursive_copy_with_weird_extra_conditions(&result, first) ||recursive_copy_with_weird_extra_conditions(&result, second); }
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
I asked that very same question.
I wrote a pretty simple piece of code that does the required work here - it's about 70 lines including main. But it uses 4 functions (which could be reduced a bit, since I have one function simply to check if it's a letter in the right range). It does ALL of the work using recursion (except for reading the string, where I cheated and used fgets()).
And to paraphrase Salem, this is like tying your shows using only your left hand - it's nothing useful to learn [unless someone has chopped off your right hand of 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 did itCbitC change
its not giving me an output
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; int res2; //start checking apha chars for string 1 if ((((str1[index1]>= 65) &&(str1[index1]<= 90))||((str1[index1]>= 97) &&(str1[index1]<= 122)))&&(index1==254)) { return 1; } if (((str1[index1]>= 65) &&(str1[index1]<= 90))||((str1[index1]>= 97) &&(str1[index1]<= 122))) { res=merge_strings(str1, index1++,str2,index2, result, index3); } else { return 0; } //end checking apha chars for string 1 //start checking apha chars for string 2 if ((((str2[index2]>= 65) &&(str2[index2]<= 90))||((str2[index2]>= 97) &&(str2[index2]<= 122)))&&(index2==254)) { return 1; } if (((str2[index2]>= 65) &&(str2[index2]<= 90))||((str2[index2]>= 97) &&(str2[index2]<= 122))) { res2=merge_strings(str1, index1,str2,index2++, result, index3); } else { return 0; } //end checking apha chars for string 2 if ((res==1)&&(res2==1)) { result[index3]=str1[index1]; if(index1<255) { res=merge_strings(str1, index1++,str2,index2, result, index3++); } result[index3]=str2[index2]; if(index2<255) { res=merge_strings(str1, index1,str2,index2++, result, index3++); } } return 1; }