my code is intended
what part is scrambled??
my code is intended
what part is scrambled??
where do i need to add
str1[index1]!='\0'
??
Somewhere in that long expression where you check for non-alpha char or non-increasing order. You also need to check str1[index1 + 1], but you need to do this carefully.Originally Posted by transgalactic2
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
i added those checks whenever i access +1 cell
its not working
when i put
1st : abdc
2nd: fghi
i get
abd|!! is valid
??
Code:#include <stdio.h> int merge(char str1[], int index1,char str2[],int index2, char result[], int index3); 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("%s is invalid.\n",result); } return 0; } int merge_strings(char str1[], int index1,char str2[],int index2, char result[], int index3) { if ((str1[index1] == '\0') && (str2[index2] == '\0')) { result[index3] = '\0'; return 1; } /* non-alpha char or non-increasing order detected */ else if ((((str1[index1]<'a')||(str1[index1]>'z'))&&((str1[index1]<'A')||(str1[index1]>'Z'))&&(str1[index1]>str1[index1+1])&&(str1[index1+1]!='\0'))&&(((str2[index2]<'a')||(str2[index2]>'z'))&&((str2[index2]<'A')||(str2[index2]>'Z'))&&(str2[index2]>str2[index2+1])&&(str1[index1+1]!='\0'))) { return 0; } /* end of str2 or str1's char is less than str2's char */ else if ((str2[index2]=='\0')||((str1[index1]<str2[index2])&&(str1[index1]!='\0'))) { result[index3] = str1[index1]; return merge_strings(str1,index1+1,str2,index2,result,index3+1); } else { result[index3] = str2[index2]; return merge_strings(str1,index1,str2,index2+1,result,index3+1); } }
He's referring to the fact that some of your lines are very long, breaking the forum table structure, and requiring scrolling sideways to see it all.
You should put a limit on the horizontal length of each line, wrapping characters that go over to the next line. I recommend an 80 character limit.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
What exactly are "those checks"? List them in point form, in words.Originally Posted by transgalactic2
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
To check for increasing order I'd actually check that the next element in both strings is greater than the last result. Checking the next cell is in order has too many special cases.
But I recommend focusing on making the function handle ordered strings correctly first.
Last edited by King Mir; 01-11-2009 at 02:17 PM.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
solved
Just for fun here's an alternative to the one you have
Code:#include <stdio.h> int merge_strings(char [], char []); int main(void) { char input[255]; char input2[255]; char res[511]; int index1, index2, flag, ch; printf("enter the first string: "); for (index1 = 0; index1 < 254 && (ch = getchar()) != EOF && ch != '\n'; ++index1) input[index1] = ch; input[index1] = '\0'; flag = merge_strings(input, NULL); if (flag) printf("%s is not valid.\n", input); else printf("%s is valid.\n", input); printf("enter the second string: "); for (index2 = 0; index2 < 254 && (ch = getchar()) != EOF && ch != '\n'; ++index2) input2[index2] = ch; input2[index2] = '\0'; flag = merge_strings(input2, NULL); if (flag) printf("%s is not valid.\n", input2); else { printf("%s is valid.\n", input2); flag = merge_strings(input, res); flag = merge_strings(input2, res+index1); printf(" input: %s\n", input); printf("input2: %s\n", input2); printf("result: %s\n", res); } return 0; } int merge_strings(char str[], char res[]) { if (str && !res) { if (((*str >= 'a') && (*str <= 'z')) || ((*str >= 'A') && (*str <= 'Z'))) { if (*(str+1)) { if (*str <= *(str+1)) return (merge_strings(++str, NULL) ? 1 : 0); else return 1; } else return 0; } else return 1; } else if (str && res) if (*res = *str) merge_strings(++str, ++res); }
Good idea, though I think that only saves one comparison per recursive call since one would then have to check for the special case where it is the first recursive call.Originally Posted by King Mir
It should already be doing that (though slightly less nicely than I would like).Originally Posted by King Mir
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.