Dustin Cook
Dustin Cook

Reputation: 1305

Ignore Certain Strings

I have some C code which reads in the contents of a HTML form from STDIN and, at the moment tokenizes the string.

fgets(formip, 1024, stdin);

pch = strtok (formip,"=&");
while (pch != NULL)
{
    printf ("%s\n",pch);
    pch = strtok (NULL, "=&");
    printf ("<br>\n");
}

This produces output like this:

checkbox,checked,value,2
checkbox,checked,value,3

Does anyone know how can I expand this to not write the "checkbox" or "value" strings?

Upvotes: 1

Views: 72

Answers (2)

Shafik Yaghmour
Shafik Yaghmour

Reputation: 158529

You can use strcmp to check if the strings are equal or not, it is important not to skip the next call to strtok otherwise you will end up in an infinite loop:

while (pch != NULL  )
{
    if( strcmp( pch, "value") != 0 && strcmp(pch, "checkbox") != 0)
    {  
       printf ("%s\n",pch);
       printf ("<br>\n");
    }
    pch = strtok (NULL, "=&");
}

Upvotes: 1

unwind
unwind

Reputation: 399949

You need to add some code that compares, and skips those unwanted strings before printing:

while(pch != NULL)
{
  if(strcmp(pch, "checkbox") == 0 || strcmp(pch, "value") == 0)
    continue;
}

Upvotes: 1

Related Questions