Thread: how to fix this pattern code..

  1. #46
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i fixed it to
    if (*input==')'){
    and it ran and said that its valid
    but when I changed the input to "(1,2) (34,5) (6 7,8)"

    It also says that its valid.
    ??

    whats incomplete?
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int is_valid(const char *input);
    const char *match_pair(const char *input);
    
    int main()
    {
        char input[] = "(1,2) (34,5) (6  7,8)";
        if (is_valid(input))
        {
            printf("%s is valid.\n", input);
        }
        else
        {
            printf("%s is invalid.\n", input);
        }
    
        return 0;
    }
    
    /* Match one or more consecutive digits and return a pointer to the first char
     * that is not a digit.
     */
    const char *match_number(const char *input)
    {
        while((*input<='9')&&(*input>='0')){
    
         input++; //increasing the  address by1
        }
    
        return input;
    }
    
    /* Match "(<number>,<number>)" pattern.
     * Return a pointer to the first char after the matched pattern, or
     * NULL if there is no match.
     */
    const char *match_pair(const char *input)
    {
           if (*input != '(')
          {
            return NULL;
          }
    
        input = match_number(input + 1); //it wiil get char '2'
    
       if (*input == ',') {   //'2' differs ','
    
             input = match_number(input + 1);
            if (*input==')'){
             return input;
            }
            else
              return NULL;
        }
        else
          return NULL;
       }
    /* Match "(<number>,<number>) (<number>,<number>) (<number>,<number>)" pattern.
     * Return 1 if there is a match and 0 otherwise.
     */
    int is_valid(const char *input)
    {
        input = match_pair(input);
        if (input == NULL)
        {
            return 0;
        }
    
        else{
    
        return 1;
        }
    
    }

  2. #47
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    whats incomplete?
    The is_valid function. At the moment, it only checks if the input begins with a pair. It should check that the input consists entirely of a string that matches the entire pattern.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #48
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    In the is_valid function i created a loop which runs forward to the next pair each time.
    but it gives me the same valid result for
    "(1,2) (34,5) (6 7,8)"

    ??
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int is_valid(const char *input);
    const char *match_pair(const char *input);
    
    int main()
    {
        char input[] = "(1,2) (34,5) (6  7,8)";
        if (is_valid(input))
        {
            printf("%s is valid.\n", input);
        }
        else
        {
            printf("%s is invalid.\n", input);
        }
    
        return 0;
    }
    
    /* Match one or more consecutive digits and return a pointer to the first char
     * that is not a digit.
     */
    const char *match_number(const char *input)
    {
        while((*input<='9')&&(*input>='0')){
    
         input++; //increasing the  address by1
        }
    
        return input;
    }
    
    /* Match "(<number>,<number>)" pattern.
     * Return a pointer to the first char after the matched pattern, or
     * NULL if there is no match.
     */
    const char *match_pair(const char *input)
    {
           if (*input != '(')
          {
            return NULL;
          }
    
        input = match_number(input + 1); //it wiil get char '2'
    
       if (*input == ',') {   //'2' differs ','
    
             input = match_number(input + 1);
            if (*input==')'){
             return input;
            }
            else
              return NULL;
        }
        else
          return NULL;
       }
    /* Match "(<number>,<number>) (<number>,<number>) (<number>,<number>)" pattern.
     * Return 1 if there is a match and 0 otherwise.
     */
    int is_valid(const char *input)
    {
    
        for(input = match_pair(input);input!=NULL;input = match_pair(input))
        {
          if (input == NULL)
            {
              return 0;
            }
    
             
        }
        return 1;
    }

  4. #49
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    In the is_valid function i created a loop which runs forward to the next pair each time.
    Trace through your loop and you will find that the return 0 statement is unreachable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #50
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i wrote this iterative function
    but on the for line it says
    |66|error: invalid operands to binary +|
    |66|warning: assignment discards qualifiers from pointer target type|

    what is the problem in the for line?
    Code:
    int is_valid(const char *input)
    {
    char *temp;
        for(temp=input;temp!=NULL;temp =temp+ match_pair(input)) //line 66
        {
          if (temp == NULL)
            {
              return 0;
            }
    
    
        }
        return 1;
    }

  6. #51
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't assign a const char * (like, say, input) to a just-plain char * (like, say, temp). You lose the const that way.

  7. #52
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    how to iterate from one end ')' to the next one??

  8. #53
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i tried to check the next ')' each time
    but its not working.

    Code:
    int is_valid(const char *input)
    {
        while (*input!=')'){
          input = match_pair(input);
          input++;
        }
          if (input == NULL)
          {
            return 0;
          }
    
          else{
    
          return 1;
          }
    
    }

  9. #54
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    i tried to check the next ')' each time
    but its not working.
    The algorithm for is_valid() is pretty simple:
    Code:
    Match "(<number>,<number>)"
    Match " "
    Match "(<number>,<number>)"
    Match " "
    Match "(<number>,<number>)"
    Check that the pointer is at the end
    If at any point there is a failure to match, return 0 immediately. The final check is to be sure that no invalid input follows the valid input, thus the function only returns 1 if that final check passes. Notice that there is no check for ')' since match_pair should handle that.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #55
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i tried this code
    it itterating from one pair to the other and when it reaches the end it checks if its ')'

    why its not working??
    Code:
    /* Match "(<number>,<number>) (<number>,<number>) (<number>,<number>)" pattern.
     * Return 1 if there is a match and 0 otherwise.
     */
    int is_valid(const char *input)
    {
        input = match_pair(input);
        if (input == NULL)
        {
            return 0;
        }
    
        /* Fill in your code here */
    
    	for(input = match_pair(input);*input != '\0';input = match_pair(input))
    	{
                       if (input == NULL)
                           {
                                    return 0;
                           }
    	}
          if (*input!=')')
                {
            return 1;
                 }   
    }
    Last edited by transgalactic2; 12-28-2008 at 10:36 AM.

  11. #56
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    One last hint before I give up on this thread:
    Code:
    for(input = match_pair(input);*input != '\0';input = match_pair(input))
    	{
                       if (input == NULL)
                           {
                                    return 0;
                           }
    	}
          if (*input!=')')
                {
            return 1;
                 }   
    }

  12. #57
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    I changed '\0' to ')'
    It didnt solved it.
    I get an endless loop.
    and then it says that it encountered an error and needed to close.
    Code:
    for(input = match_pair(input);*input != ')';input = match_pair(input))
    	{
                       if (input == NULL)
                           {
                                    return 0;
                           }
    	}
          if (*input!=')')
                {
            return 1;
                 }

  13. #58
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    *sigh*
    Okay, I think you've tried hard enough but just cannot get it. Here's my sample answer:
    Code:
    /* Match one or more "(<number>,<number>)" pairs separated by a space.
     * Return 1 if there is a match and 0 otherwise.
     */
    int is_valid(const char *input)
    {
        /* Match "(<number>,<number>)" */
        input = match_pair(input);
        if (input == NULL)
        {
            return 0;
        }
    
        /* Match zero or more " (<number>,<number>)" */
        while (*input != '\0')
        {
            if (*input != ' ')
            {
                return 0;
            }
    
            input = match_pair(input + 1);
            if (input == NULL)
            {
                return 0;
            }
        }
    
        return 1;
    }
    Originally I thought that you only wanted to match three number pairs separated by a space, but apparently you want to match one or more number pairs separated by a space, hence I modified is_valid() to check for that.

    If you need specific explanation, just ask.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #59
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    and i did the last other option
    its not working
    ??
    Code:
    int is_valid(const char *input)
    {
        input = match_pair(input);
        if (input == NULL)
        {
            return 0;
        }
    
        /* Fill in your code here */
    
    	for(input = match_pair(input);*input != '\0';input = match_pair(input))
    	{
                       if (input == NULL)
                           {
                                    return 0;
                           }
    	}
          if (*input!='\0')
                {
            return 1;
                 }   
    }

  15. #60
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    for "(1,2) (34,5) (6 7,8)" it says that its invalid which is correct
    but when i put
    "(1,2) (34,5) (67,8)" it says that its invalid which is wrong

    ??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 04-20-2009, 07:35 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM