Thread: recursive string operation..

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Newsflash for you: stack overflow!
    You do nothing but recursively call your function over and over again.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by transgalactic2 View Post
    i didnt check for ascending order i need this part to work so i will start working on the
    ascending order part
    gotcha!

  3. #18
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i cant use my own external functions either

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What is your exit condition for merging the strings?

    --
    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. #20
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i check index<255
    if its larger then its not going further.

  6. #21
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    define your terminal condition when you reach the end of the string to prevent stack overflow.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    My opinion is that you need to redo this whole code / function. It's way too complicated and has ands and ors put out without any ( and ), even. And what does all those indexes have to do with things?
    Why don't you start by writing it all in pseudo code?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i changed the first if in the merging part to
    index1<255
    but its still gives me the same result
    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, index++,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, index,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, index++,str2,index2, result, index3++);
             }
    
             result[index3]=str2[index2];
             if(index2<255)
             {
                 res=merge_strings(str1, index,str2,index2++, result, index3++);
             }
    
         }
         return 1;
    }
    Last edited by transgalactic2; 01-08-2009 at 03:39 PM.

  9. #24
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    i cant use my own external functions either
    I'm not talking about EXTERNAL functions - or do you mean to say that the assignment clearly says that this has to be done in ONE FUNCTION? If so, I'd say it's worse than learning to tie your shoe-laces with one hand - you are now having to do it with one hand and your thumb and forefinger tied together with a sticking plaster. That is, it is utter lunacy to do this in ONE FUNCTION.

    Edit: I would double-check with your teacher (or whatever) that you are actually supposed to do all of this in ONE FUNCTION. And if that is the case, go look for another school and ask for your money back [assuming you paid for the course], as it is not at ALL a good 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.

  10. #25
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by itCbitC View Post
    define your terminal condition when you reach the end of the string to prevent stack overflow.
    i did define a terminal condition

    its
    Code:
    if ((((str1[index1]>= 65) &&(str1[index1]<= 90))||((str1[index1]>= 97) &&(str1[index1]<= 122)))&&(index==254))
         {
    
             return 1;
         }
    and

    Code:
    if(index1<255)

  11. #26
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by transgalactic2 View Post
    i check index<255
    if its larger then its not going further.
    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().

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Eh, I give up. transgalactic2 is impossible to teach anyway. As usual.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by matsp View Post
    I'm not talking about EXTERNAL functions - or do you mean to say that the assignment clearly says that this has to be done in ONE FUNCTION? If so, I'd say it's worse than learning to tie your shoe-laces with one hand - you are now having to do it with one hand and your thumb and forefinger tied together with a sticking plaster. That is, it is utter lunacy to do this in ONE FUNCTION.

    --
    Mats
    I completely agree with you

    I could write external function
    and later merge it with this one.

    but the point is the recursion
    i cant see where i going wrong.

  14. #29
    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 changed the index=0 to index
    now its giving me the exe error again

    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;
          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, index++,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, index,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, index++,str2,index2, result, index3++);
             }
    
             result[index3]=str2[index2];
             if(index2<255)
             {
                 res=merge_strings(str1, index,str2,index2++, result, index3++);
             }
    
         }
         return 1;
    }

  15. #30
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
          int res,index;
          int res2;
          //start checking apha chars for string 1
         if ((((str1[index1]>= 65) &&(str1[index1]<= 90))||((str1[index1]>= 97) &&(str1[index1]<= 122)))&&(index==254))
    index is not set to any value at this point...

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

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