Arduino

Arduino strcmp() Function

Arduino is programmed using C or C++ language; most of the syntax of both these languages are the same. Functions used in C language can easily be implemented in Arduino programming one of them is strcmp which compares two different strings characters. Let’s find out how to do this in Arduino IDE.

strcmp() Function

The strcmp() function in Arduino programming compares two different strings and returns three values depending upon the characters inside the string. The string compare function checks the ASCII values of characters present in both strings and returns either a positive, negative or zero number in output.

The strcmp() compares both string 1 and string 2 character by character. It will keep on comparing until it finds a dissimilar character between both strings. If the first character of both strings is equal it will move towards the second one and will keep on comparing until it finds a dissimilar character. In case if not a single dissimilar character is found, zero will be returned.

Syntax

Syntax of strcmp() function is given below:

int strcmp (const char * string1, const char * string2 );

In the syntax stated above string1 and string2 are two strings whose characters are to be compared one by one.

Parameter Values

The strcmp() function takes two parameters:

  • string1: Pointer to the first string which is to be compared.
  • string2: Pointer to second string which is to be compared.

Return Values

The characters on keyboard have unique ASCII values so the strcmp() functions return three different values depending upon the string characters:

  • Positive value: If string 1’s characters have a greater ASCII value than string 2’s first differing character.
  • Negative value: If string 1’s characters have a smaller ASCII value than string 2’s first differing character.
  • Zero: If string 1 and string 2 are equal.

This function will compare every character in the string with every character of another string and will continue until there comes any dissimilar character that is present in only one string based on ASCII values.

Example Code

The strcmp() function works by comparing ASCII values of the characters, for reference see table below we have taken two characters i & u. Both have different ASCII values with “i” having 105 and u value equal to 117. Which means the ASCII value of i is smaller than u.

Similarly capital letters I and U will have ASCII value having U greater ASCII than I.

ASCII Character DEC HEX OCT
i 105 69 151
u 117 75 165

Using an example, let’s learn how strcmp() works.

void setup(){

int Comparison_Result;

Serial.begin(9600); /*Define Baud Rate*/

char* string1 = "Linuxhint"; /*String1 starts with small i (ASCII Code 105 in Decimal)*/

char* string2 = "Linuxhunt"; /*String2 starts with small u (ASCII Code 117 in Decimal)*/

Comparison_Result = strcmp(string1, string2); /*Compare Both Strings*/

Serial.print("Output After Comparing Two strings = ");

/*Will Returns negative number ASCII value of unmatch character of string 2 is greater than string 1*/

Serial.println(Comparison_Result);

}

void loop(){

}

In this code first we define a new variable output to store return value of strcmp() function. Then we initialized serial communication by defining baud rate. Next two strings are defined.

The strcmp() function will compare both strings and store the output in variables created at start. To print the output on serial-monitor Serial.println function is used.

Output

We can categorize output in three different ways:

1. When Output is Positive

Here the output is positive because string 1 ASCII value is greater than string 2.

2. When Output is Negative

Here the output is negative because string 1 ASCII value is smaller than string 2.

3. When Output is Zero

Here the output is zero because string 1 is equal to string 2.

Note: Multiple other functions are also available that compare two strings such as:

  • String equals() function.
  • compareTo() function.
  • String Comparison Operators(<, =, >).

Conclusion

This write-up will help you to enhance and improve Arduino programming skills. If someone has a basic understanding of the C/C++ language, it will be easy to program the Arduino board. The strcmp() function compares two different strings and returns either positive, negative or zero numbers.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.