for example:
istringstream ss("hello hi here haha");
string p;
while (iss >> p)
{
if (p == "hello")
statement1;
else if (p == hi)
statement2;
}
here parsing is used so what can b used in c for doing this?
for example:
istringstream ss("hello hi here haha");
string p;
while (iss >> p)
{
if (p == "hello")
statement1;
else if (p == hi)
statement2;
}
here parsing is used so what can b used in c for doing this?
Here is an example code that is your snippet translated to C:
#include <stdio.h>
#include <string.h>
int main ()
{
char s[] ="hello hi here haha";
char *tok;
char *last;
tok = strtok_r(s, " ", &last);
while (tok != NULL) {
if(!strcmp(tok, "hello"))
statement1;
else if(!strcmp(tok, "hi"))
statement2;
tok = strtok_r(NULL, " ", &last);
}
return 0;
}
Update I changed the calls of strtok
to strtok_r
as recommended by Adam Rosenfield in the comments.
Something like this?
char* ss = "hello hi here hahah";
int i=0;
while (ss[i] != '\0')
{
while (ss[i] != ' ' && ss[i] != '\0')
++i;
char* p[40];
memcpy(p,ss,i);
if (p == "hello")
statement1;
else if (p = "hi")
statement2;
}