# Thread: pattern checking in a string question..

1. ## 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;
}```

2. Originally Posted by transgalactic2
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. 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.

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

4. where did leave java syntax?

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

6. Code:
```int checkPoly(char[] str,int length)
{
int result;
result=false;```

7. 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. In C99 with <stdbool.h> included the use of false and mixing declarations and code is allowed.

9. 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. Originally Posted by laserlight
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. 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).

12. 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. Originally Posted by transgalactic2
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.

14. in the end i need it to be a "string" (char array)

what is the problem??

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