JIYIK CN >

Current Location:Home > Learning > PROGRAM > C++ >

Arduino strcmp function

Author:JIYIK Last Updated:2025/04/16 Views:

In this tutorial, we will discuss strcmp()comparing two strings using the compare function in Arduino.


Arduino strcmp()Functions

strcmp()Function compares two strings in Arduino. strcmp()The function compares the ASCII values ​​of the characters present in two strings and then returns three types of output values ​​based on the ASCII values ​​of the characters.

The characters on the keyboard have unique ASCII values, for example athe ASCII value of the character is 65. Following is strcmp()the basic syntax of the function.

output = strcmp(string1, string2);

The above syntax will return a negative number if all the characters present in both the strings are identical, 0if the characters in the first string that do not match the characters of the second string have lower ASCII values ​​than the characters in the second string, it will return a positive number if the ASCII values ​​of the non-matching characters of the first string are greater than the characters of the second string.

If the difference between the ASCII values ​​of the first two characters is zero, strcmp()the function will move on to the next character, and so on, and when all characters have been compared, it will also return 0, which will indicate that the two strings are equal.

If the ASCII values ​​of the two characters are not equal to 0, the function will stop and return the difference between the ASCII values ​​of the current unmatched characters.

For example, let's define two identical strings and strcmp()compare them using the function in Arduino.

See the code below.

int output;
void setup() {
  char* string1 = "hello";
  char* string2 = "hello";
  output = strcmp(string1, string2);
  Serial.begin(9600);
  Serial.println(output);
}
void loop() {}

Output:

0

strcmp()The input to the function should be a constant string. In the above code, we use the Arduino serial monitor to display strcmp()the output of the function.

Serial.begin()The function is used to initialize the serial monitor and Serial.println()the function prints the given value on the serial monitor window.

We can also use the output of a function in conditional statements strcmp(), such as ifthe if statement to perform specific tasks, such as if the output is equal to 0, we can print on the serial monitor that two strings are equal.

We can also use other functions of Arduino to compare two strings such as compareTo()and equals()functions. Check out this link for more details on string comparison in Arduino.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Stopping a loop in Arduino

Publish Date:2025/04/16 Views:150 Category:C++

This tutorial will discuss the methods to stop a loop in Arduino. There are two types of loops in Arduino: one is the void loop() provided by default and the other is created by the user in it. The loop created by the user can be ended easi

Arduino prints to console

Publish Date:2025/04/16 Views:68 Category:C++

This tutorial will discuss printing text or variables on the console using the Arduino IDE's serial monitor. Arduino using the serial monitor to print to the console The Arduino IDE has a console at the bottom, but we cannot print anything

Arduino Array Length

Publish Date:2025/04/16 Views:181 Category:C++

Arrays are fundamental data structures in programming, and in Arduino, they play a key role when storing and manipulating data. Often, you'll find yourself needing to know the size or length of an array, especially when working on complex p

Arduino 2D Array

Publish Date:2025/04/16 Views:108 Category:C++

In this tutorial, we will discuss about 2D arrays in Arduino. We will discuss how to initialize a 2D array and use it to store data. 2D Array Initialization in Arduino Two-dimensional array initialization is very similar to one-dimensional

Printing Character Array in Arduino

Publish Date:2025/04/16 Views:59 Category:C++

This tutorial will discuss printing character array using loop in Arduino. Serial.println() Define int and print character arrays in Arduino In Arduino, if we int initialize an array using keyword, we have to use a loop to print its element

Arduino Square Wave Generator

Publish Date:2025/04/16 Views:181 Category:C++

digitalWrite() This tutorial will discuss generating a square wave using the function in Arduino . Arduino Square Wave Generator A square wave consists of maximum and minimum values, and the transitions between these values ​​are instan

Splitting a string in Arduino

Publish Date:2025/04/16 Views:188 Category:C++

substring() This tutorial will discuss splitting a string using the function in Arduino . substring() Splitting a string in Arduino using the function Arduino provides an inbuilt function substring() to split a given string. We can split th

Comparing Strings in Arduino

Publish Date:2025/04/16 Views:137 Category:C++

compareTo() This tutorial will discuss comparing two strings using the function in Arduino . compareTo() Comparing strings using the Arduino function To compare two strings in Arduino, we can use compareTo() the function of the string objec

Concatenating strings in Arduino

Publish Date:2025/04/16 Views:190 Category:C++

This tutorial will discuss concat() concatenating two strings using the function or the append operator in Arduino. concat() Concatenate strings using the Arduino function We can concat() concatenate two strings in Arduino using the concate

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial