i am asked to build a function which does both
they clash together on thee indexs
i need to merge the from index1=0 index2=0 index3=0
but when the function gets to this part
it just gives the indexes different numbers i dont know why
Printable View
i am asked to build a function which does both
they clash together on thee indexs
i need to merge the from index1=0 index2=0 index3=0
but when the function gets to this part
it just gives the indexes different numbers i dont know why
Most likely reason that's happening is because merge_strings() returns a value to the caller and when the stack for this program is collapsed the index values get clobbered or overwrite each other. Try writing it such that it doesn't return anything to the caller ie void.
i cant i need it to be integer type
here is both of them
merge only merges them
merge_string checks for alpha an ascending order
i dont know how to combine them
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(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)
{
int check,check2,check3,check4;
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;
}
////////////////////////////////////////////////////////////////////////
if (str1[index1+1]=='\0')
{
return 1;
}
if (str1[index1]>=str1[index1-1])
{
check=merge_strings(str1,index1+1,str2,index2,result,index3);
if (check==0)
{
return 0;
}
else
{
check3=1;
}
}
else
{
return 0;
}
////////////////////////////////////////////////////////////////
if (str2[index2+1]=='\0')
{
return 1;
}
if (str2[index2]>=str2[index2-1])
{
check=merge_strings(str1,index1,str2,index2+1,result,index3);
if (check==0)
{
return 0;
}
else
{
return 1;
}
}
else
{
return 0;
}
/////////////////////////////////////////////////////////////////////////////
}//end function
int merge(char str1[], int index1,char str2[],int index2, char result[], int index3)
{
int check,check2,check3,check4;
if(str2[index2]=='\0')
{
result[index3+1]='\0';
return 1;
}
else
{
result[index3]=str2[index2];
check=merge(str1,index1,str2,index2+1,result,index3+1);
}
if(str1[index1]=='\0')
{
return 1;
}
else
{
result[index3]=str1[index1];
check=merge(str1,index1+1,str2,index2,result,index3+1);
}
return 1;
}//end function
Maybe it is time to use some classical ideas in recursion. What is your base case, and what is the recursive step?
i tried many ways to make them on functions which first checks
then combines
there is no base case thing here
because its not only one operation returning true or false
it the alpha chech +the descending check +merging
they all differ from one another
for a example this is a simple case
where thee is only one base case
but all it does is the ascending check
Code:int merge_strings(char str1[], int index1,char str2[],int index2, char result[], int index3)
{
int check3;
if (str1[index1+1]=='\0')
{
return 1;
}
else
{
if(str1[index1]<=str1[index1+1])
{
check3=merge_strings(str1,index1+1,str2,index2,result,index3);
if (check3==0)
{
return 0;
}
else
{
printf("ok\n");
return 1;
}
}
else
{
return 0;
}
}
//end ascending check
}//end function
Of course there is a base case, otherwise the recursion would never end. I thought about the base case and the recursive step, and finished implementing merge_strings a few minutes later because I knew exactly what I was doing.Quote:
Originally Posted by transgalactic2
Let's try this again: what is the base case and the recursive step?
the base case thinking catches only for one operation
there is no way to combine all three operations into this one base case model.
here are both of my functions doing the checking and the combining operations apart
i need to combine them into 1
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(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)
{
int check,check2,check3,check4;
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;
}
////////////////////////////////////////////////////////////////////////
if (str1[index1+1]=='\0')
{
return 1;
}
if (str1[index1]>=str1[index1-1])
{
check=merge_strings(str1,index1+1,str2,index2,result,index3);
if (check==0)
{
return 0;
}
else
{
check3=1;
}
}
else
{
return 0;
}
////////////////////////////////////////////////////////////////
if (str2[index2+1]=='\0')
{
return 1;
}
if (str2[index2]>=str2[index2-1])
{
check=merge_strings(str1,index1,str2,index2+1,result,index3);
if (check==0)
{
return 0;
}
else
{
return 1;
}
}
else
{
return 0;
}
/////////////////////////////////////////////////////////////////////////////
}//end function
int merge(char str1[], int index1,char str2[],int index2, char result[], int index3)
{
int check,check2,check3,check4;
if(str2[index2]=='\0')
{
result[index3+1]='\0';
return 1;
}
else
{
result[index3]=str2[index2];
check=merge(str1,index1,str2,index2+1,result,index3+1);
}
if(str1[index1]=='\0')
{
return 1;
}
else
{
result[index3]=str1[index1];
check=merge(str1,index1+1,str2,index2,result,index3+1);
}
return 1;
}//end function
ok my base case of all three operation is
if the index of the array reaches '\0' char
that means that all the chars before it are ok
and return 1.
am i correct?
Grr... I see that I did not fully understand what you were trying to do.
You need to state very clearly what merge and merge_strings are supposed to do. Comment your code, and indent it properly.
I am trying to build a function which gets 3 strings str1,str2,result .
the function checks if all the chars in strings str1 ans str2 are alpha type and if each one of them
are sorted in an ascended order regarding their ascii value.
for example
str1:"abcds" (legal string)
str2:"ghjk" (legal string)
If they both legal then it needs to merge them into the result array
so for our exampe:
str1:"abcds" (legal string)
str2:"ghjk" (legal string)
result:"abcdsghjk"
here is the function called merge_strings which only checks if each string are alpha type
and if each string is in ascending order.
here is my merge function which only merges both of th string into the third reult string.Code:
int merge_strings(char str1[], int index1,char str2[],int index2, char result[], int index3)
{
int check,check2,check3,check4;
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;
}
if (str1[index1+1]=='\0')
{
return 1;
}
if (str1[index1]>=str1[index1-1])
{
check=merge_strings(str1,index1+1,str2,index2,result,index3);
if (check==0)
{
return 0;
}
else
{
check3=1;
}
}
else
{
return 0;
}
if (str2[index2+1]=='\0')
{
return 1;
}
if (str2[index2]>=str2[index2-1])
{
check=merge_strings(str1,index1,str2,index2+1,result,index3);
if (check==0)
{
return 0;
}
else
{
return 1;
}
}
else
{
return 0;
}
}//end function
this is my my main function and the interface .Code:int merge(char str1[], int index1,char str2[],int index2, char result[], int index3)
{
int check,check2,check3,check4;
if(str2[index2]=='\0')
{
result[index3+1]='\0';
return 1;
}
else
{
result[index3]=str2[index2];
check=merge(str1,index1,str2,index2+1,result,index3+1);
}
if(str1[index1]=='\0')
{
return 1;
}
else
{
result[index3]=str1[index1];
check=merge(str1,index1+1,str2,index2,result,index3+1);
}
return 1;
}//end function
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(input,0,input2,0,result, 0);
if (flag)
{
printf("%s is valid.\n",result);
}
else
{
printf("%s is invalid.\n",result);
}
return 0;
}
Let me try to rephrase that.
This function returns 1 if str1 and str2 both only contain alphabetic characters sorted in strictly increasing order, and merges the characters, placing the result in the string result, otherwise it returns 0:
This function returns 1 if str1 and str2 both only contain alphabetic characters sorted in strictly increasing order, otherwise it returns 0:Code:int merge(char str1[], int index1,char str2[],int index2, char result[], int index3);
That begs the question: why is merge_strings named merge_strings instead of say, check_alpha, and why does merge_strings have the result and index3 parameters when they are unused?Code:int merge_strings(char str1[], int index1,char str2[],int index2, char result[], int index3);
Actually, check_alpha should only take one string and one index as parameters, following the principle that a function should do one thing and do it well.
no.
merge and merge_strings needs to be one function called merge_strings which
returns 1 if str1 and str2 both only contain alphabetic characters sorted in strictly increasing order, and merges the characters, placing the result in the string result, otherwise it returns 0.
what i got now is two functions merge_strings which i should call check_alpha_ascending
and merge which merges them
Okay, it is much clearer to me now.Quote:
Originally Posted by transgalactic2
By the way, you are learning this in school, right? Because if this is some voluntary course that you are taking, you should have quit long ago. Your instructor does not understand the basic principles of software engineering, and probably does not understand computer science either.
EDIT:
On second thought, I'll give the fellow the benefit of the doubt. I still rather have students learn how to separate code into functions that do one thing and do it well, but in this case a single function could still accomplish the task recursively without that much difficulty. Again, it is a matter of determining the base case and recursive step.