Thread: recursive string operation..

  1. #31
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by itCbitC View Post
    redo the code with proper indentation; index is a local variable and everytime merge_strings() is invoked index is reset to zero due to this int res,index=0; in merge_strings().
    i was told here that index=0 is a problem
    so i removed it

    do i need to put index=0
    ??

  2. #32
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by transgalactic2 View Post
    i was told here that index=0 is a problem
    so i removed it

    do i need to put index=0
    ??
    i didnt understand him correctly.
    i will change the code

  3. #33
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i changed it
    like itCbitC said.
    i dont get the exe error but i dont have any output either

    ??
    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,index=0;
          int res2;
          //start checking apha chars for string 1
         if ((((str1[index1]>= 65) &&(str1[index1]<= 90))||((str1[index1]>= 97) &&(str1[index1]<= 122)))&&(index==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 ((((str1[index1]>= 65) &&(str1[index1]<= 90))||((str1[index1]>= 97) &&(str1[index1]<= 122)))&&(index==254))
           {
    
             return 1;
         }
         if (((str1[index1]>= 65) &&(str1[index1]<= 90))||((str1[index1]>= 97) &&(str1[index1]<= 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[index];
             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;
    }

  4. #34
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, now the question is: Where does index change? And what do you actually use index for in your 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.

  5. #35
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i removed index. and i changed the code to the proper "index" from the signature

    it doesnt give me an output at all

    ??
    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 ((((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)))
         {
    
             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;
    }
    Last edited by transgalactic2; 01-08-2009 at 04:25 PM.

  6. #36
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, AGAIN: What is your exit condition. What do you have to do to get there?

    --
    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.

  7. #37
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    this is my exit condition
    Code:
    if ((((str1[index1]>= 65) &&(str1[index1]<= 90))||((str1[index1]>= 97) &&(str1[index1]<= 122)))&&(index1==254))
         {
    
             return 1;
         }
    it returns 1 when all the chars are ok.

    in the merging part when the index reaches 255 it doesnt go to the if again
    thus stopping the recursion and returning 1

  8. #38
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Doesn't merging mean that you pick characters from one or another string so that the resulting string would also be in ascending order? Otherwise what is the purpose of checking whether the input strings are in ascending order?

    Or is this task simply a collection of pretty arbitrary requirements (I don't see any relevance in the A...Z check) in hope that no other teacher has ever asked anybody to write such a bizarre program (and you can't search for the code on the internet)?

    this is my exit condition
    This exit condition would appear to be always false (the last character in the max-length string shouldn't be anything other than '\0'), or you have already passed the end of the string and the result of that is undefined...

    Also, could you for a start write this using a simple loop. It is usually trivial to turn a single loop into a recursion.
    Last edited by anon; 01-08-2009 at 04:39 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #39
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    this is my exit condition
    Code:
    if ((((str1[index1]>= 65) &&(str1[index1]<= 90))||((str1[index1]>= 97) &&(str1[index1]<= 122)))&&(index1==254))
         {
    
             return 1;
         }
    it returns 1 when all the chars are ok.

    in the merging part when the index reaches 255 it doesnt go to the if again
    thus stopping the recursion and returning 1
    And how long is the string you have entered?

    --
    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.

  10. #40
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by anon View Post
    Doesn't merging mean that you pick characters from one or another string so that the resulting string would also be in ascending order? Otherwise what is the purpose of checking whether the input strings are in ascending order?

    Or is this task simply a collection of pretty arbitrary requirements (I don't see any relevance in the A...Z check) in hope that no other teacher has ever asked anybody to write such a bizarre program (and you can't search for the code on the internet)?



    This exit condition would appear to be always false (the last character in the max-length string shouldn't be anything other than '\0'), or you have already passed the end of the string and the result of that is undefined...
    i do need to pick up from the last place of the first string
    thats why i put index3++ on both calls

  11. #41
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    ok i understand that str2 is written over str1 places

    i dont know how to fix it

  12. #42
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by transgalactic2 View Post
    i removed index. and i changed the code to the proper "index" from the signature

    it doesnt give me an output at all

    ??
    Code:
    //start checking apha chars for string 2
        if ((((str1[index1]>= 65) &&(str1[index1]<= 90))||((str1[index1]>= 97) &&(str1[index1]<= 122)))&&(index1==254))
    what do you think should go here? str1 or str2?
    maybe that's why you are not getting any output at all.
    post the entire code again, one more time (hopefully better indented).

  13. #43
    The larch
    Join Date
    May 2006
    Posts
    3,573
    So you want to copy first string and append the other? Can you do this with a loop? The other things like testing for order and character values is just a distraction that has little to do with the recursion (except perhaps you have to reserve the return value for signalling error).

    Also, does it have to be all just one function, besides main?

    Otherwise it might go something like this:

    Code:
    int recursive_copy_with_weird_extra_conditions(char** result, const char* input);
    
    int funny_merge(const char* first, const char* second, char* result)
    {
        return recursive_copy_with_weird_extra_conditions(&result, first) ||recursive_copy_with_weird_extra_conditions(&result, second);
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  14. #44
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by anon View Post
    Also, does it have to be all just one function, besides main?
    I asked that very same question.

    I wrote a pretty simple piece of code that does the required work here - it's about 70 lines including main. But it uses 4 functions (which could be reduced a bit, since I have one function simply to check if it's a letter in the right range). It does ALL of the work using recursion (except for reading the string, where I cheated and used fgets()).

    And to paraphrase Salem, this is like tying your shows using only your left hand - it's nothing useful to learn [unless someone has chopped off your right hand of course].

    --
    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.

  15. #45
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i did itCbitC change
    its not giving me an output
    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 (((str2[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;
    }

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