Thread: pattern checking in a string question..

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

    pattern checking in a string question..

    This function needs to check if the sting is a polynomial .
    It check it for some sort pattern. If this string is not builded by this pattern then it return
    0. else it returns 1

    i translated this function from java

    can you tell me what pattern it checks in this string??
    (variable length is the length of the string)
    Code:
     
    int checkPoly(char[] str,int length)
    	{
    	  int result;
    	  result=false;
              int index;
               int index2;
                 int index3;
    
    
     
    
    	  for( index=0;((index<length)&& (((str[length-1]>'0')&&(str[length-1]<'9')))));index++)
    	   {
    		  if((str[index]=='(')&&(str[index+1]!='('))
    		  {
    			
    			  for (index2=0;index2<length;index2++)
    			  {
    				  if(str[index2]==')')
    				  {                                        
    					  if((str[index2+1]=='x')&&(str[index2+2]=='^')&&((str[index2+3]>'0')&&(str[index2+3]<'9'))))
    
    					  {
    						  result=1;
    						  index=length;
    						  for (index3=index2+2;((index3<length)&&(((length)-(index3+2)!=0)));index3++)
    						  {							 
    							  if(str[index3]=='(')
    							  {
    								  if(str[index3-1]=='+')
    								  {									  
    									  index3=length;
    									  result=1;
    								  }
    								  else
    								  {
    									  index3=length;
    									  index2=length;				
    									  result=0;
    								  }
    								  
    							  }else{}
    						  }
    					  }else
    					  {
    						  result=0;
    						  index2=length;
    						  index=length;
    						  
    					  }
    				  }else{}
    			  }
    		  }else {
    			  result=0;
    			  index=length;
    			  }
    		 
           }
    	  return result;
    	}
    Last edited by transgalactic2; 12-26-2008 at 12:12 PM.

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by transgalactic2 View Post
    This function needs to check if the sting is a polynomial .
    It check it for some sort pattern. If this string is not builded by this pattern then it return
    0. else it returns 1

    i translated this function from java

    can you tell me what pattern it checks in this string??
    (variable length is the length of the string)
    You did not quite finish your translation. There is some stuff in there that is not C code. And which string are you referring to here?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    i translated this function from java
    You need to be aware of differences in syntax, e.g., the syntax for array declaration.

    Quote Originally Posted by transgalactic2
    can you tell me what pattern it checks in this string??
    (variable length is the length of the string)
    You need to make some effort on your part first, e.g., tell us what you think the function does and the reasoning behind your answer.

    One way to help you test your answer is to actually run the program with sample input, and to do that your program must at least be syntactically correct (or you compile and run the Java version of the program). It would also help if you indented your code properly and reduced the number of superfluous parentheses, e.g.,
    Code:
    int checkPoly(char str[], int length)
    {
        int result = 0;
        int index;
        int index2;
        int index3;
    
        for (index = 0;
            (index < length) && (str[length - 1] > '0') && (str[length - 1] < '9');
            index++)
        {
            if ((str[index] == '(') && (str[index + 1] != '('))
            {
                for (index2 = 0; index2 < length; index2++)
                {
                    if (str[index2] == ')')
                    {
                        if ((str[index2 + 1] == 'x') && (str[index2 + 2] == '^')
                            && (str[index2 + 3] > '0') && (str[index2 + 3] < '9'))
                        {
                            result = 1;
                            index = length;
                            for (index3 = index2 + 2;
                                (index3 < length) && ((length - (index3 + 2)) != 0);
                                index3++)
                            {
                                if (str[index3] == '(')
                                {
                                    if (str[index3 - 1] == '+')
                                    {
                                        index3 = length;
                                        result = 1;
                                    }
                                    else
                                    {
                                        index3 = length;
                                        index2 = length;
                                        result = 0;
                                    }
                                }
                            }
                        }
                        else
                        {
                            result = 0;
                            index2 = length;
                            index = length;
                        }
                    }
                }
            }
            else
            {
                result = 0;
                index = length;
            }
        }
        return result;
    }
    (Note that I removed the empty else blocks, but left some superfluous parentheses in even though I would normally remove them.)
    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

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    where did leave java syntax?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    where did leave java syntax?
    I have already given you a hint: array declaration syntax. You should have tried to compile and run your code before posting it on a forum.
    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

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Code:
    int checkPoly(char[] str,int length)
    	{
    	  int result;
    	  result=false;

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so i need to change char str[] >> char[] str

    i can see its something like:
    (num*x^num + ..) +

    but i am not sure
    i cant check this thing on a compiler
    because its a pattern
    it doesnt give a hint like "you are close but you should fix the col"
    it gives true or false

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In C99 with <stdbool.h> included the use of false and mixing declarations and code is allowed.
    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

  9. #9
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    I meant 0 and 1.
    my point is i could test ten inputs and i coudnt see i one input is closer to the pattern then the
    other ones.

    I can see its something like:
    (numx^num + ..) +

    But i am not sure.

  10. #10
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by laserlight View Post
    In C99 with <stdbool.h> included the use of false and mixing declarations and code is allowed.
    That is true, however I was assuming (perhaps incorrectly) that the OP had not made a conscious decision to leave that in, but rather was an oversight.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    i cant check this thing on a compiler
    because its a pattern
    it doesnt give a hint like "you are close but you should fix the col"
    it gives true or false
    Well, you already know that the function is supposed to return 1 if the string matches some polynomial expression syntax and 0 otherwise. From what I understand, what you want to do is to precisely determine what is this pattern that the function checks for. This means carefully looking over the function's implementation.

    When you have come to a conclusion, you can then test if you are wrong by running the program with carefully selected input and checking if the output is what you expect. If the output is unexpected then you know that your conclusion must be wrong, otherwise you have more evidence that your conclusion is correct (but unless you exhaustively test all possible input you cannot be absolutely certain that your conclusion is correct merely by testing).
    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

  12. #12
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i am trying to run this function

    and on this line i was told that
    |12|warning: char format, different type arg (arg 2)|
    Code:
    scanf("%c",&str[index]);
    and another warning says
    |16|warning: implicit declaration of function `checkPoly'|

    whats what do i need to change?

  13. #13
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by transgalactic2 View Post
    i am trying to run this function

    and on this line i was told that
    |12|warning: char format, different type arg (arg 2)|
    Code:
    scanf("%c",&str[index]);
    and another warning says
    |16|warning: implicit declaration of function `checkPoly'|

    whats what do i need to change?
    For the first warning, you are asking scanf to get a char, but obstensibly passing it an address to an int array.

    For the second warning, you are trying to call the function from main without a function prototype.

    Code:
    int checkPoly(char str[], int length);
    ...
    int main(void)
    {
    ....
    }
    You could also just make sure the function code comes after the main function in the same source file.
    Last edited by kermit; 12-26-2008 at 02:29 PM.

  14. #14
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    in the end i need it to be a "string" (char array)

    what is the problem??

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    in the end i need it to be a "string" (char array)
    Use fgets() instead. I have an example in the thread printing from output...
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  5. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 06:44 PM