# SMS comparison problem

**URL:** <https://forum.arduino.cc/t/sms-comparison-problem/918299>\
**Category:** Programming\
**Created:** [October 25, 2021, 11:33am UTC](https://forum.arduino.cc/t/sms-comparison-problem/918299 "2021-10-25T11:33:54Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![luanxxpx](https://avatars.discourse-cdn.com/v4/letter/l/8dc957/32.png) [@luanxxpx](https://forum.arduino.cc/u/luanxxpx)\
**Post date:** [October 25, 2021, 11:33am UTC](https://forum.arduino.cc/t/sms-comparison-problem/918299/1 "2021-10-25T11:33:54Z")

</div>

Hi guys im trying to compare the Content of SMS.  
But i dont know how i should do this.  
I know that in the example they use:  
" while( (c = sms.read()) ! = -1)  
{  
Serialprintln ((char)c) ;  
}  
"  
but i should compare the SMS and watch if it matches my keyword for Example "ON"  
and i dont need to write it some where like in the example  
and then do stuff but I just dont get it how i should code this...  
pls help

I tried somethings like  
if( c == "ON")  
{  
....  
}

---

<div class="post-metadata">

**Author:** ![blh64](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blh64/32/605986_2.png) [@blh64](https://forum.arduino.cc/u/blh64)\
**Post date:** [October 25, 2021, 11:45am UTC](https://forum.arduino.cc/t/sms-comparison-problem/918299/2 "2021-10-25T11:45:05Z")

</div>

Look at this tutorial: [Serial Input Basics - updated](http://forum.arduino.cc/index.php?topic=396450.0) to learn how to read in a string and then act upon it. The tutorial is designed for Serial input, but will work for SMS input just as well.

---

<div class="post-metadata">

**Author:** ![luanxxpx](https://avatars.discourse-cdn.com/v4/letter/l/8dc957/32.png) [@luanxxpx](https://forum.arduino.cc/u/luanxxpx)\
**Post date:** [October 25, 2021, 12:32pm UTC](https://forum.arduino.cc/t/sms-comparison-problem/918299/3 "2021-10-25T12:32:08Z")

</div>

i know how to read in a string so u mean i could do something like:  
" for( i = 0; i\<7; i ++)  
{  
Sms\_String [i] = sms.read() ;  
}  
if "Sms\_String == "GO ON""  
and here we have the same problem i cant compare with a string cause idk what to put in the braces [?] to use the whole string i dont need to use the braces right just the name so it doesnt have a Index?

---

<div class="post-metadata">

**Author:** ![blh64](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blh64/32/605986_2.png) [@blh64](https://forum.arduino.cc/u/blh64)\
**Post date:** [October 25, 2021, 2:15pm UTC](https://forum.arduino.cc/t/sms-comparison-problem/918299/4 "2021-10-25T14:15:40Z")

</div>

string comparison is done with `strcmp()` or to find a substring within a string `strstr()`

---

<div class="post-metadata">

**Author:** ![330R](https://avatars.discourse-cdn.com/v4/letter/3/d78d45/32.png) [@330R](https://forum.arduino.cc/u/330R)\
**Post date:** [October 25, 2021, 3:00pm UTC](https://forum.arduino.cc/t/sms-comparison-problem/918299/5 "2021-10-25T15:00:48Z")

</div>

Remember, when you use `strcmp()`, that it can return one of 3 values. Zero is for equality.

---

<div class="post-metadata">

**Author:** ![luanxxpx](https://avatars.discourse-cdn.com/v4/letter/l/8dc957/32.png) [@luanxxpx](https://forum.arduino.cc/u/luanxxpx)\
**Post date:** [October 26, 2021, 6:19am UTC](https://forum.arduino.cc/t/sms-comparison-problem/918299/6 "2021-10-26T06:19:55Z")

</div>

come on guys i have no clue how to solve this. And its a very important project to me.  
strcmp just compares characters but sms.read() gives a int back so this cant work.

---

<div class="post-metadata">

**Author:** ![Whandall](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/whandall/32/222457_2.png) [@Whandall](https://forum.arduino.cc/u/Whandall)\
**Post date:** [October 26, 2021, 7:47am UTC](https://forum.arduino.cc/t/sms-comparison-problem/918299/7 "2021-10-26T07:47:14Z")

</div>

> [@luanxxpx](#):
>
> strcmp just compares characters but sms.read() gives a int back so this cant work.

Wrong.

Where was this argument when you tried:

> [@luanxxpx](#):
>
> if (Sms\_String == "GO ON")

---

<div class="post-metadata">

**Author:** ![luanxxpx](https://avatars.discourse-cdn.com/v4/letter/l/8dc957/32.png) [@luanxxpx](https://forum.arduino.cc/u/luanxxpx)\
**Post date:** [October 26, 2021, 7:55am UTC](https://forum.arduino.cc/t/sms-comparison-problem/918299/8 "2021-10-26T07:55:59Z")

</div>

Yeah ik this also dont work but that shows how desperate I am.  
how can i solve this?

---

<div class="post-metadata">

**Author:** ![Whandall](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/whandall/32/222457_2.png) [@Whandall](https://forum.arduino.cc/u/Whandall)\
**Post date:** [October 26, 2021, 7:58am UTC](https://forum.arduino.cc/t/sms-comparison-problem/918299/9 "2021-10-26T07:58:01Z")

</div>

By using `strcmp()` or maybe `strncmp()` on the buffered output,  
just like you tried with the direct comparison, which does not work.

`strstr()` could be useful also, it depends on the format of the message.

---

<div class="post-metadata">

**Author:** ![luanxxpx](https://avatars.discourse-cdn.com/v4/letter/l/8dc957/32.png) [@luanxxpx](https://forum.arduino.cc/u/luanxxpx)\
**Post date:** [October 26, 2021, 8:14am UTC](https://forum.arduino.cc/t/sms-comparison-problem/918299/10 "2021-10-26T08:14:45Z")

</div>

```cpp
    String SmS [7];
    String Command [7];
    Command[1] = "O";
    Command[2] = "N";
    int i = 0;
    
   for(;i<7;i++)
   {
      SmS[i] = sms.read();
   }
   if ( (strcmp(SmS,Command)))
   {
    Serial.println("\nOutput now ON");
   }

```

ive used this now but there is a ERROR saying: "cannot convert 'arduino::String\*' to 'const char\*' for argument '1' to 'int strcmp(const char\*, const char\*)'"  
and btw could you tell me the diffrence between `strcmp()` and strncmp()? thx

---

<div class="post-metadata">

**Author:** ![Whandall](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/whandall/32/222457_2.png) [@Whandall](https://forum.arduino.cc/u/Whandall)\
**Post date:** [October 26, 2021, 8:15am UTC](https://forum.arduino.cc/t/sms-comparison-problem/918299/11 "2021-10-26T08:15:50Z")

</div>

Forget all that String crap and use a char array for storing the message,  
which would be obvious, if you had looked at

> [@blh64](#):
>
> Look at this tutorial: [Serial Input Basics - updated](http://forum.arduino.cc/index.php?topic=396450.0) to learn how to read in a string and then act upon it.

---

<div class="post-metadata">

**Author:** ![luanxxpx](https://avatars.discourse-cdn.com/v4/letter/l/8dc957/32.png) [@luanxxpx](https://forum.arduino.cc/u/luanxxpx)\
**Post date:** [October 26, 2021, 8:31am UTC](https://forum.arduino.cc/t/sms-comparison-problem/918299/12 "2021-10-26T08:31:37Z")

</div>

```cpp
    char SmS [3];
    const char Command [3] = "ON";
    
   for(int i = 0;i<3;i++)
   {
      SmS[i] = sms.read();
   }
   if ( SmS == Command)
   {
    Serial.println("\nOutput now ON");
   }

```

ive used now char arrays and direct comparison but the if case still doesnt get true ☹

---

<div class="post-metadata">

**Author:** ![Whandall](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/whandall/32/222457_2.png) [@Whandall](https://forum.arduino.cc/u/Whandall)\
**Post date:** [October 26, 2021, 8:33am UTC](https://forum.arduino.cc/t/sms-comparison-problem/918299/13 "2021-10-26T08:33:26Z")

</div>

Do you read what we write? You were already told how to deal with that

> [@Whandall](#):
>
> By using `strcmp()` or maybe `strncmp()` on the buffered output,

I miss the closing zero on the SmS char array, it is filled with chars from some function.  
Is it really guaranteed, that the third char is a zero?

---

<div class="post-metadata">

**Author:** ![luanxxpx](https://avatars.discourse-cdn.com/v4/letter/l/8dc957/32.png) [@luanxxpx](https://forum.arduino.cc/u/luanxxpx)\
**Post date:** [October 26, 2021, 8:34am UTC](https://forum.arduino.cc/t/sms-comparison-problem/918299/14 "2021-10-26T08:34:49Z")

</div>

so i can use stringcomparison to arrays? okay u said i can forget about the string stuff

---

<div class="post-metadata">

**Author:** ![Whandall](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/whandall/32/222457_2.png) [@Whandall](https://forum.arduino.cc/u/Whandall)\
**Post date:** [October 26, 2021, 8:36am UTC](https://forum.arduino.cc/t/sms-comparison-problem/918299/15 "2021-10-26T08:36:17Z")

</div>

> [@luanxxpx](#):
>
> so i can use stringcomparison to arrays?

C-strings **are** char arrays, with a zero signaling the end of it.  
`strcmp(), strncmp(), strstr()` work on those cstrings (zero terminated char arrays).

> [@luanxxpx](#):
>
> u said i can forget about the string stuff

No.

I said you should get rid of the String crap, which is a big difference.

---

<div class="post-metadata">

**Author:** ![luanxxpx](https://avatars.discourse-cdn.com/v4/letter/l/8dc957/32.png) [@luanxxpx](https://forum.arduino.cc/u/luanxxpx)\
**Post date:** [October 26, 2021, 11:51am UTC](https://forum.arduino.cc/t/sms-comparison-problem/918299/16 "2021-10-26T11:51:23Z")

</div>

thx guys i made it and thank you for your patience 😅

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 24, 2022, 11:51am UTC](https://forum.arduino.cc/t/sms-comparison-problem/918299/17 "2022-04-24T11:51:33Z")

</div>

Closed on April 24, 2022, 11:51am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
