-
i did the ascending part
i cant think of how to merge them recursevly
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,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]=='\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 (str1[index1]=='\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
{
return 1;
}
}
else
{
return 0;
}
}//end function
-
the problem with this asceding code
is that it checkson the first run cell -1
its a memory bug
??
how to fix it
-
why this code works????
i clearly need to have a buffer underrun
because in the first run i accsess cell -1
why its not giving me this bug??
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,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]=='\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 (str1[index1]=='\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
{
return 1;
}
}
else
{
return 0;
}
}//end function
-
how do i say to it
skip the first cell
dont check the first cell [0] compared to the previous one?
-
how to do this ascending check
its not working
Code:
int merge_strings(char str1[], int index1,char str2[],int index2, char result[], int index3)
{
if (str1[index1]=='\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
-
First of all I think you're trying to cram too much into the same function, code modularization is what you need to do. matsp gave you the keys to the kingdom but you are not heeding his advice. Suggest you start all over again and do keep it simple.
-
i am keeping this simple thats why i am working only on the ascending part
Code:
int merge_strings(char str1[], int index1,char str2[],int index2, char result[], int index3)
{
if (str1[index1]=='\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
-
Can you post the entire updated code here.
-
actually i think i got the solution to the ascending part
i tried a various input and it gives me the correct answer
:)
the big problem is how to merge this two arrays into one
one after another into the big result array in a recursive way??
i cant image it
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,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
-
i wrote this merging code
but its putting only the first one
why??
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("%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;
}
else
{
result[index1]=str1[index1];
check=merge_strings(str1,index1+1,str2,index2,result,index3);
}
if(str2[index2]=='\0')
{
return 1;
}
else
{
result[index2]=str2[index2];
check=merge_strings(str1,index1,str2,index2+1,result,index3);
}
return 1;
}//end function
-
because when you read the stream for the second string, there is a \n awaiting to be read, resulted by the 'enter' key you pressed when submitting the first string. so that is your first read character.
-
the getchar clean the buffers
there is no problem there
i put printf after i entered them
both of them were printed fine
-
this merging code is 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,lnd;
printf("enter the first string \n");
for (index = 0; index < 254 && (ch = getchar()) != '\n' && ch >=0; ++index)
{
input[index] = ch;
}
input[index] = '\0';
printf("%s is valid.\n",input);
printf("enter the second string \n");
for (index = 0; index < 254 && (ch = getchar()) != '\n' && ch >=0; ++index)
{
input2[index] = ch;
}
input2[index] = '\0';
printf("%s is valid.\n",input2);
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)
{
int check,check2,check3,check4;
if(str1[index1]=='\0')
{
return 1;
}
else
{
result[index1]=str1[index1];
check=merge_strings(str1,index1+1,str2,index2,result,index3);
}
if(str2[index2]=='\0')
{
return 1;
}
else
{
result[index2]=str2[index2];
check=merge_strings(str1,index1,str2,index2+1,result,index3);
}
return 1;
}//end function