Thread: recursive string operation..

  1. #61
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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

  2. #62
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    the problem with this asceding code
    is that it checkson the first run cell -1

    its a memory bug
    ??

    how to fix it

  3. #63
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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

  4. #64
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    how do i say to it
    skip the first cell

    dont check the first cell [0] compared to the previous one?

  5. #65
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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

  6. #66
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    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.

  7. #67
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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

  8. #68
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Can you post the entire updated code here.

  9. #69
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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
    Last edited by transgalactic2; 01-09-2009 at 02:37 PM.

  10. #70
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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

  11. #71
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    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.

  12. #72
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    the getchar clean the buffers

    there is no problem there

    i put printf after i entered them

    both of them were printed fine
    Last edited by transgalactic2; 01-09-2009 at 04:06 PM.

  13. #73
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM