The strcmp() function is an essential string comparison tool in the Arduino programming language. This comprehensive guide will explain what it does, how to use it, and provide expanded examples for real-world applications.

What Does the Arduino strcmp() Function Do?

The strcmp() function compares two strings character-by-character based on their ASCII values. It returns an integer indicating whether the first string is less than, equal to, or greater than the second string:

  • Positive integer: String 1 is greater than String 2
  • Negative integer: String 1 is less than String 2
  • 0: String 1 equals String 2

This order is determined by comparing the ASCII values of each character, starting from the first character of each string. Comparing stops at the first non-matching character.

strcmp() Syntax

The syntax for strcmp() is:

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

Where:

  • string1 – Pointer to the first string for comparison
  • string2 – Pointer to the second string for comparison

The function returns an integer indicating the order of string1 relative to string2, as described above.

Practical strcmp() Examples

Let‘s look at some practical examples of using strcmp() in Arduino sketches.

Example 1: Simple String Comparison

This example compares two simple strings and prints the return value of strcmp():

char* string1 = "Hello";
char* string2 = "Hello";

int result = strcmp(string1, string2);

Serial.println(result); // Prints 0 since strings are equal

Example 2: Comparing User Input

This example reads two strings from the serial port and compares them:

char input1[20]; 
char input2[20];

void setup() {

Serial.begin(9600);

Serial.println("Enter string 1:"); readSerial(input1);

Serial.println("Enter string 2:"); readSerial(input2);

}

void loop() {

int result = strcmp(input1, input2);

if(result < 0) { Serial.println("String 1 is less than String 2"); } else if(result > 0) { Serial.println("String 1 is greater than String 2"); } else { Serial.println("String 1 equals String 2"); }

delay(5000);

}

void readSerial(char result[]) { byte bytes = Serial.readBytesUntil(‘\n‘, result, 20); result[bytes] = 0;
}

This allows testing strcmp() with any two strings input by the user.

Example 3: Password Checking

Here is an example of using strcmp() to compare an input string to a hardcoded correct password:

const char* password = "secret"; 

void setup(){

Serial.begin(9600);

}

void loop(){

char input[20];

Serial.println("Enter password:"); readSerial(input);

if(strcmp(input, password) == 0) { Serial.println("Password correct"); } else { Serial.println("Wrong password"); }

delay(5000);

}

This technique can be used to implement simple security.

When to Use strcmp()

The strcmp() function is ideal for:

  • Comparing user input to validated options
  • Implementing simple password systems
  • Testing string equality in sketches
  • Sorting algorithms requiring string comparison

It provides an efficient way to check if two strings differ or match exactly.

Conclusion

The Arduino strcmp() function compares two strings based on their ASCII values, returning an integer indicating their order. It‘s a vital tool for string testing and conditional code execution. This guide covered proper syntax, examples of real-world usage, and good applications for strcmp() in Arduino programming. Let me know in the comments if you have any other questions!

Similar Posts