recursive string operation..

This is a discussion on recursive string operation.. within the C Programming forums, part of the General Programming Boards category; I need to build a function which gets two strings checks if their chars are alpha type in ascending order. ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    recursive string operation..

    I need to build a function which gets two strings checks if their chars are alpha type
    in ascending order.
    If they both are then it needs to merge them in a third d string.

    I cant call external functions.

    The whole process must be in one function
    and no iterative loops
    and only in a recursive way.

    I tried to build this function.

    i think it works
    but just after i input
    abc in the first string and def into the second one

    it does nothing and says that my program the EXE file
    encountered a problem
    and needs to be closed

    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("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(index<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 01:38 PM.

  2. #2
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    this function is without the ascending order check
    i wanted to check one part

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    IMO, revisit the algorithm you have tried to to code because there are a few things you seem to have left out from the program implementation.
    Also it's way better to compare chars instead of their value as in str1[index1]>= 'A' is the preferred way instead of str1[index1]>= 65. Just my 2c.
    Last edited by itCbitC; 01-08-2009 at 02:05 PM.

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    what things i leftout?

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,602
    Run-Time Check Failure #3 - The variable 'index' is being used without being initialized.
    Listen to your compiler warnings!
    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.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
         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;
         }
    Could we not do this a bit simpler, since there is two exactly identical checks here...

    --
    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. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by Elysia View Post
    Run-Time Check Failure #3 - The variable 'index' is being used without being initialized.
    Listen to your compiler warnings!
    i have zero warnings.

    index3 does not need to be initialized because it comes from
    the signature of the function.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by transgalactic2 View Post
    what things i leftout?
    For starters you need to loop through the string comparing each element with the next one and making sure that they are in alphabetically ascending order.
    ATM you are not doing, that is, not comparing adjacent elements to make sure that each element is an alphabet and is lexically less than its successor.
    Last edited by itCbitC; 01-08-2009 at 02:20 PM.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    index does not come from the signature of your function, and in merge_strings you most definitely use it without it being initialized.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,602
    Quote Originally Posted by transgalactic2 View Post
    i have zero warnings.
    Then turn up the warnings! Uninitialized variables are quite serious.

    index3 does not need to be initialized because it comes from the signature of the function.
    Not talking about index3. Talking about index.
    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.

  11. #11
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by matsp View Post
    Code:
         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;
         }
    Could we not do this a bit simpler, since there is two exactly identical checks here...

    --
    Mats
    The first "if" is the stop case were it reaches the end of the string.
    The second "if" is for keep checking if the chars till now are correct and it checks the
    next one.

    i cant think of doing it in another way.

    where is the problem in the algorithm?

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by transgalactic2 View Post
    The first "if" is the stop case were it reaches the end of the string.
    The second "if" is for keep checking if the chars till now are correct and it checks the
    next one.

    i cant think of doing it in another way.

    where is the problem in the algorithm?
    boc how do you make sure that the string is in ascending order? compare adjacent elements perhaps; and i think matsp is suggesting code modularization.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    The first "if" is the stop case were it reaches the end of the string.
    The second "if" is for keep checking if the chars till now are correct and it checks the
    next one.

    i cant think of doing it in another way.

    where is the problem in the algorithm?
    The problem is that it's too complicated. So check the common thing first, then use a separate if-statement inside that to avoid rechecking four rather complicated conditions.

    Also, you can't call any external function, but as I read it, you should be able to call YOUR OWN LOCAL functions. So perhaps you can write one that checks if a char is a letter A-Z, a-z. And please use character constants rather than 65, 90, 97, 122, etc. It makes it MUCH easier to read if some constant is 'A' than 65.

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

  14. #14
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by tabstop View Post
    index does not come from the signature of your function, and in merge_strings you most definitely use it without it being initialized.
    i changed
    Code:
    int index=0;
    now when input abc in the first string and def in the second
    it end without showing anything
    ??
    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(index<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. #15
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i didnt check for ascending order i need this part to work so i will start working on the
    ascending order part

Page 1 of 5 12345 LastLast
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, 02:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21