Comparing 2 strings

Hi,

The function below is not working properly, how can i compare 2 strings?
I send “temperat” to the arduino through the serial port, so the IF statement should return the analog value, only it doesnt work, it only prints back:
“Received: temperat”

How should i alter the IF statement?
I already tried comparing 2 char arrays
(received and command)

char command[8];
char received[8] = {‘t’,‘e’,‘m’,‘p’,‘e’,‘r’,‘a’,‘t’}’

if (Serial.available() == 8)
{
for (int i=0; i < 8; i++)
{
command *= Serial.read(); *

  • }*
  • if (command == “temperat”)*
  • {*
  • val = analogRead(0);*
  • Serial.print(val,DEC);*
  • }*
  • Serial.print("Received: ");*
  • Serial.print(command);*
  • }*

There are a few functions you can call to compare strings. strcmp is the most basic, it takes the two strings to be compared as parameters and returns 0 if the strings are equal.

you would use it as follows:

if (Serial.available() >= 8) // this was == 8, more than one command may be waiting
{
for (int i=0; i < 8; i++)
{
command *= Serial.read(); *

  • }*

  • if (strcmp(command, “temperat”) == 0) // test to see if the two strings are equal*

  • {*

  • val = analogRead(0);*

  • Serial.print(val,DEC);*

  • }*

  • Serial.print("Received: ");*

  • Serial.print(command);*

  • }*
    BTW, i changed the test for serial available so that it will work if more than one command is received at the serial port when the sketch checks to see if charactors are available

There are a few functions you can call to compare strings. strcmp is the most basic, it takes the two strings to be compared as parameters and returns 0 if the strings are equal.

BTW, i changed the test for serial available so that it will work if more than one command is received at the serial port when the sketch checks to see if charactors are available

Cool thanks for the fast response ;D

Would it be appropriate to add strcmp to...

http://www.arduino.cc/en/Reference/Extended

... and the keywords list?

Does any library need to be #included to use strcmp?

Is there a different place to find strcmp? Any other "hidden" keywords?

> Would it be appropriate to add strcmp to... http://www.arduino.cc/en/Reference/Extended The reference page on strings http://www.arduino.cc/en/Reference/String would be a good place to mention the comparison functions

>Does any library need to be #included to use strcmp? String handling functions like strcmp are declared in string.h but the Arduino environment seems to include this automatically.

>Is there a different place to find strcmp? Any other "hidden" keywords? A good book on C should list these library functions and you can find a references to C library functions by doing an online search for words like: c language reference