Thread: recursive string operation..

  1. #46
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by matsp View Post
    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
    what functions to write??

    could i merge them later into 1?

  2. #47
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    what functions to write??

    could i merge them later into 1?
    Anything that is ever repeated in code (that is more than one or two lines) can be made into a separate function. In this case, I would check if the character is alpha in a function, for example - or better yet, check if the whole string is alpha [that's what I did when I solved your task last night (it's no good talking about solutions unless you have actually solved the problem, and I don't think I've ever written string manipulation recursively before) - I did the check if the string is ALL alpha into a function, that also checks that the characters are in the right order (which I made a bit messy)].

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

  3. #48
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i have written this
    external function.
    Code:
    int check_valid(char ch)
    {
        if (((ch>= 'a')&&(ch<='z'))||((ch>='A') &&(ch<='Z')))
         {
             return 1;
         }
         else 
         {
             return 0;   
         }
    }
    i dont know how to integrate it into the big function in a recursive way.
    if i could do loops then i simply
    Code:
    int status,flag=-1;
    for(i=0;i<index1;i++)
    {
         status=check_valid(str1[i]);
         if (status==0)
         {
             flag=0;
         }
    }
    if (flag==0)
    {
        statut=0;  //i am using status again but for the use of putting the final result of a string
    }
    else
    {
       status=1;
    }

    but its a recursion
    i cant put this code in the loop.

  4. #49
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Recursion IS a loop - it just uses a different mechanism to jump to the code...

    --
    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. #50
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    how do i think of this mechanism

  6. #51
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i built a function in the start of this post
    i changed it later because there was some logical errors that i didnt mean to commit
    but still i dont know what the problem in its algorithm

    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 (((str1[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;
    }

  7. #52
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If we split the problem into parts: Check that all letters are alphanumeric - how would you go about doing that, using recursion?

    Think about it.
    Start by thinking of the exit condition: When are you done?
    The second step is to figure out how you split a check the letters into something you can do recurisvely - this can take the form "Test the first letter". How do you know if the rest of the string is alphanumeric?

    By the way, have you checked with your tutor if you MUST do this with one function, or if you are allowed multiple functions as long as they fulfill the criteria of not calling outside functions and always using recursion?

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

  8. #53
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i need to use do this whole operation in one function
    using only recursion

  9. #54
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i will try to build it again

  10. #55
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i built this simple recursive function for checking only the first string

    why its 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;
        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 check;
        if (str1[index1]=='\0')
        {
              return 1;
        }
    
        if (((str1[index1]>='a')&&(str1[index1]<='z'))||((str1[index1]>='A')&&(str1[index1]<='Z')))
        {
             check=merge_strings(str1,index1++,str2,index2,result,index3);
             if (check==0)
             {
                 return 0;
             }
             else
             {
                 return 1;
             }
        }
        else
        {
             return 0;
        }
    
    
    }

  11. #56
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Try tracing your function by adding a printf() to see what is going into your merge_string() function. [Or if you have a debugger available, set a breakpoint at the beginning of the 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.

  12. #57
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    ok this simple function works

    the problem is with the return
    i built it so it will return 1 or 0 depending if the string is correct

    my return needs to be associated with the total resolt
    how do i change it so i will get the resolt in one local variable??
    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;
        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
             {
                 return 1;
             }
        }
        else
        {
             return 0;
        }
    
    
    }

  13. #58
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    on one hand I need a "return" to return the resolt for the next char
    but on the other hand I need to remove it and put a local variable to collect the resolt
    ??

  14. #59
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    this input worked
    Last edited by transgalactic2; 01-09-2009 at 11:05 AM.

  15. #60
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i finished the ascii checking part its not returning anything just puting 1 into
    the local variable
    if all checks will be correct i will return 1.

    how to do the ascending checking part ??
    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;
        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;
        }
    
    
    
    }//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