I'm using the following technique to find the number of operators in a mathematical string.
for (int index = 0; index < [self.evaluateString length]; index++) {
unichar chars = [self.evaluateString characterAtIndex:index];
if (chars == '+' || chars == '-' || chars == '*' || chars == '/' ||chars == '^') {
self.operatorCount++;
}
}
My trainer says this method is not very good. I would like to know is there any better/more elegant method to do this. Thanks.
It seems that string is to be evaluated. Part of that evaluation is parsing. During this process mathematical operators are identified and could be counted.
The advantage over simple character counting would be to tell apart a
3 - 1(operator) from a-1(negative number literal).