Different Ways to Compare Strings in C++
This section will discuss the different ways to compare the given strings in the C++ programming language. The comparison of the string determines whether the first string is equal to another string or not. Example: HELLO and Hello are two different strings.
There are different ways to compare the strings in the C++ programming language, as follows:
- Using strcmp() function
- Using compare() function
- Using Relational Operator
- Using For loop and If statement
- Using user-defined function
strcmp() function
The strcmp() is a pre-defined library function of the string.h header file. The strcmp() function compares two strings on a lexicographical basis. This means that the strcmp() function starts comparing the first string with the second string, character by character until all characters in both strings are the same or a NULL character is encountered.
Syntax
Parameters:
leftstr: It defines the characters of the left string.
rightstr: It defines the characters of the right string.
Returns:
The leftstr string compares each character with the second string from the left side till the end of both strings. And, if both the strings are equal, the strcmp() function returns strings are equal. Else, the strings are not equal.
Let’s create a program to compare strings using the strcmp() function in C++.
Program1.cpp
Output
String 1: Welcome to Tutor Aspire String 2: Welcome to Tutor Aspire Both strings are equal. String 3: tutoraspire String 4: tutoraspire The strings are not equal.
compare() function
The compare() function is a pre-defined library function of the C++ language. The compare() function compares two given strings and returns the following results based on the matching cases:
- If both the strings are the same, the function returns 0.
- If the character value of the first string is smaller than the second string, the function returns < 0.
- If the second string is greater than the first string, the function returns greater than 0 or >0.
Syntax
Let’s create a simple program to compare two strings using the compare() function in C++.
Program2.cpp
Output
1st Run: Enter the string 1: Program Enter the string 2: program Program is smaller than program string 2nd Run: Enter the string 1: APPLE Enter the string 2: APPLE Both strings are equal.
Relational Operator
It is the operator used to compare two strings or numerical values in C++. C++ has different types of relational operators such as ‘==’, ‘!=’, >, < operator. But here, we use only two operators such as ‘==’ equal to and ‘!=’ not equal to a relational operator to compare the string easily.
Syntax
Compare two strings using the Equal to (==) operator in C++
Equal To (==) operator: It is used to check the equality of the first string with the second string.
Let’s create a program to compare strings using the double equal to (==) operator in C++.
Program3.cpp
Output
Enter the String 1: tutoraspire Enter the String 2: tutoraspire String is not equal.
2nd Execution:
Enter the String 1: Program Enter the String 2: Program String is equal.
Compare two strings using the Not Equal To (!=) Relational Operator
Let’s create a program to compare whether the strings are equal or not using the Not Equal To (!=) operator in C++.
Program4.cpp
Output
Enter the String 1: tutoraspire Enter the String 2: tutoraspire String is not equal.
2nd Run:
Enter the String 1: HELLO Enter the String 2: HELLO String is equal.
Compare two strings using for loop and if statement in C++
Program5.cpp