Anything that is ever repeated in code (that is more than one or two lines) can be made into a separate function. In this case, I would check if the character is alpha in a function, for example - or better yet, check if the whole string is alpha [that's what I did when I solved your task last night (it's no good talking about solutions unless you have actually solved the problem, and I don't think I've ever written string manipulation recursively before) - I did the check if the string is ALL alpha into a function, that also checks that the characters are in the right order (which I made a bit messy)].
--
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 have written this
external function.
i dont know how to integrate it into the big function in a recursive way.Code:int check_valid(char ch) { if (((ch>= 'a')&&(ch<='z'))||((ch>='A') &&(ch<='Z'))) { return 1; } else { return 0; } }
if i could do loops then i simply
Code:int status,flag=-1; for(i=0;i<index1;i++) { status=check_valid(str1[i]); if (status==0) { flag=0; } } if (flag==0) { statut=0; //i am using status again but for the use of putting the final result of a string } else { status=1; }
but its a recursion
i cant put this code in the loop.
Recursion IS a loop - it just uses a different mechanism to jump to the code...
--
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.
how do i think of this mechanism
i built a function in the start of this post
i changed it later because there was some logical errors that i didnt mean to commit
but still i dont know what the problem in its algorithm
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 (((str1[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; }
If we split the problem into parts: Check that all letters are alphanumeric - how would you go about doing that, using recursion?
Think about it.
Start by thinking of the exit condition: When are you done?
The second step is to figure out how you split a check the letters into something you can do recurisvely - this can take the form "Test the first letter". How do you know if the rest of the string is alphanumeric?
By the way, have you checked with your tutor if you MUST do this with one function, or if you are allowed multiple functions as long as they fulfill the criteria of not calling outside functions and always using recursion?
--
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 need to use do this whole operation in one function
using only recursion
i will try to build it again
i built this simple recursive function for checking only the first string
why its not working??
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 check; if (str1[index1]=='\0') { return 1; } if (((str1[index1]>='a')&&(str1[index1]<='z'))||((str1[index1]>='A')&&(str1[index1]<='Z'))) { check=merge_strings(str1,index1++,str2,index2,result,index3); if (check==0) { return 0; } else { return 1; } } else { return 0; } }
Try tracing your function by adding a printf() to see what is going into your merge_string() function. [Or if you have a debugger available, set a breakpoint at the beginning of the 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.
ok this simple function works
the problem is with the return
i built it so it will return 1 or 0 depending if the string is correct
my return needs to be associated with the total resolt
how do i change it so i will get the resolt in one local variable??
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", input); } else { printf("%s is invalid.\n",input); } return 0; } int merge_strings(char str1[], int index1,char str2[],int index2, char result[], int index3) { int check; if (str1[index1]=='\0') { return 1; } if (((str1[index1]>='a')&&(str1[index1]<='z'))||((str1[index1]>='A')&&(str1[index1]<='Z'))) { check=merge_strings(str1,index1+1,str2,index2,result,index3); if (check==0) { return 0; } else { return 1; } } else { return 0; } }
on one hand I need a "return" to return the resolt for the next char
but on the other hand I need to remove it and put a local variable to collect the resolt
??
this input worked
Last edited by transgalactic2; 01-09-2009 at 11:05 AM.
i finished the ascii checking part its not returning anything just puting 1 into
the local variable
if all checks will be correct i will return 1.
how to do the ascending checking part ??
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", input); } else { printf("%s is invalid.\n",input); } return 0; } int merge_strings(char str1[], int index1,char str2[],int index2, char result[], int index3) { int check,check2; if (str1[index1]=='\0') { return 1; } if (((str1[index1]>='a')&&(str1[index1]<='z'))||((str1[index1]>='A')&&(str1[index1]<='Z'))) { check=merge_strings(str1,index1+1,str2,index2,result,index3); if (check==0) { return 0; } else { check=1; } } else { return 0; } //////////////////////////////////////////////////////// if (str2[index2]=='\0') { return 1; } if (((str2[index2]>='a')&&(str2[index2]<='z'))||((str2[index2]>='A')&&(str2[index2]<='Z'))) { check2=merge_strings(str1,index1,str2,index2+1,result,index3); if (check2==0) { return 0; } else { check2=1; } } else { return 0; } }//end function