Curnelious Curnelious - 4 months ago 28
C Question

Divided a string by one of a few options

I have a

char
array in which I would like to loop, where its divided by all math operations ,

for example, right now I am looking only for
=
sign , so

for (char *p = strtok(data,"="); p != NULL; p = strtok(NULL, " "))
{
numberOfChars++;
}


I would like to loop where not only
"="
is the token , but it could be one of these also :
+,-,*,\,=


so that we increment in a loop the
numberOfChars
for every one of these.

so for :
a = b + c
we will get 3 .

LPs LPs
Answer

You can symply loop through the string

for (size_t i=0; i < strlen(data); i++)
    {
        if ((data[i] == '+') || 
            (data[i] == '-') || 
            (data[i] == '*') || 
            (data[i] == '\') || 
            (data[i] == '='))
        {
            numberOfChars++;
        }
    }