Test if a string contains a string

Hi :slight_smile:

I'm trying to use a GSM shield to do different operations sent via sms. The contents of the sms is stored in a string. If the SMS contains certain words, certain tasks are to be performed.

I tried doing this with the strstr() function, but I'm not sure how to use it as a boolean, since the function returns pointers. If the word to look for is not found in the string to search, it returns a null pointer.
If I write

If(strstr(smsbuffer,"codeword"){do stuff;}

that always returns as true, whether the sms contained the word or not. Apparently the null pointer is not equal to zero. How should I do this? Is there a better way than strstr()?

See strstr()

Yes, that is the function I'm using, as you can see if you read my post above :).
My question is how I get that function to work as a boolean. Or if there is a better function to use.

Return Value
A pointer to the first occurrence in str1 of the entire sequence of characters specified in str2, or a null pointer if the sequence is not present in str1

Yes, I read that. I'm afraid I don't quite understand the concept of "Null pointer" and how to test if that's what's beeing returned though. It is apparently not the same as 0, or the finction I wrote above should work. Neither can I write

if(!(strstr(smsbuffer,"codeword")=NULL)){dothestuff;}

since that just gives me the old "left operand" error.

So the short version of my question is: how do I test the function strstr() to see if it returns a null pointer?

'=' is an assignment. '==' to test for equality.

Oh god. I wish this thread was deleted and never spoken about again. I wish I could do to it what Terry Pratchett posthumously did to his hard drive: run it over with a steamroller.

This is what happens when you take a few months off coding and wasn't very good at it to begin with. Sorry for wasting time and forum space with my brain fart.

Mixe:
Oh god. I wish this thread was deleted and never spoken about again. I wish I could do to it what Terry Pratchett posthumously did to his hard drive: run it over with a steamroller.

This is what happens when you take a few months off coding and wasn't very good at it to begin with. Sorry for wasting time and forum space with my brain fart.

NO, this is what happens when you don't expend any effort in debugging your code.

Paul