-1

Im having entering string array and each array has one expression. I want to split the expressions with considering all conditional operators. My expression may be like str[0] , str[1], str[2]

   FIC >= 1 && PIC == 1
   FCV<=0.2 && FCC>=12

Now i have to split first string in to 'FIC >=1' ' PIC ==1'

i can do this by using strtok (str[0],"&&");

And again i have split 'FIC >= 1' in to string as 'FIC' and '>=' and '1' . Then i have separate function in my application to get the value of 'FIC' and i will check this condition is true.If yes then i have to do some action.

But this expression will not be the same, some expression has more number of && operators and more conditional operators. How can i split this

Anu
  • 905
  • 4
  • 23
  • 54
  • 1
    `strtok` is intended for simple tokenization. For example, one intended use is to find all the sequences of characters that are separated by spaces, tabs, or new-line characters, such as separated “abc def ghi” into “abc”, “def”, and “ghi”. It only recognizes individual characters, which are listed in the second string. E.g., to separate at spaces, tabs, and new-line characters, you would use a second string containing one space, one tab, and one new-line. Repeating “&” will not make `strtok` recognize “&&”. – Eric Postpischil Feb 05 '18 at 05:49
  • https://stackoverflow.com/questions/4319166/c-function-for-separate-a-string-in-array-of-chars – theboringdeveloper Feb 05 '18 at 05:49
  • 1
    It looks like what you are trying to do is to parse an expression grammar. This is substantially more complicated than separating a string into tokens. There are textbooks written about it, and you need more sophisticated code to do it properly. – Eric Postpischil Feb 05 '18 at 05:49

1 Answers1

1

If you know the separator strings, you could use the library function strstr.

    const char * str1 = "FIC >= 1 && PIC == 1";
    const char * sep = "&&";
    char *p = NULL, *q = NULL, *r = NULL;
    int len = 0;

    p = strstr(str1, sep);
    len = p - str1;
    q = calloc(len + 1, sizeof(char));
    strncpy(q, str1, len);
    len = strlen(str1) - len - strlen(sep);
    r = calloc(len + 1, sizeof(char));
    p += strlen(sep);
    strcpy(r, p);

Now q and r point to buffers that hold the two parts of the input string. Note that strstr returns NULL if sep isn't found in str1, so if you can't guarantee the sep is in the string, check for NULL before proceeding.

bruceg
  • 2,433
  • 1
  • 22
  • 29