This part will explore various methods for comparing strings in the C++ programming language. String comparison is used to ascertain if one string matches another. For instance, "HELLO" and "Hello" are considered distinct 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 function is a built-in function found in the string.h header file. It is designed to compare two strings based on their lexicographical order. Essentially, strcmp compares each character of the first string with the corresponding character of the second string until either all characters match or a NULL character is reached.
Syntax
int strcmp ( const char *leftstr, const char *rightstr );
Parameters:
The leftstr function specifies the characters within the left string.
It specifies the characters within the rightstr.
Returns:
The leftstr function compares individual characters starting from the second character of each string towards the end. If the strings match, the strcmp function indicates that the strings are equal. Otherwise, it signifies that the strings are not identical.
Let's develop a software to contrast strings by utilizing the strcmp function in C++.
Program1.cpp
#include <iostream>
using namespace std;
#include <string.h>
int main ()
{
// declare strings
const char *str1 = " Welcome to JavaCppTutorial";
const char *str2 = " Welcome to JavaCppTutorial";
const char *str3 = " JavaCppTutorial";
const char *str4 = " Javacpptutorial";
cout << " String 1: " << str1 << endl;
cout << " String 2: " << str2 << endl;
// use strcmp() function to validate the strings are equal
if (strcmp (str1, str2) == 0)
{
cout << " \n Both strings are equal. " << endl;
}
else
{
cout << " The strings are not equal. " << endl;
}
cout << " \n String 3: " << str3 << endl;
cout << " String 4: " << str4 << endl;
// use strcmp() function to validate the strings are equal
if (strcmp (str3, str4) == 0)
{
cout << " \n Both strings are equal. " << endl;
}
else
cout << " \n The strings are not equal. ";
return 0;
}
Output
String 1: Welcome to JavaCppTutorial
String 2: Welcome to JavaCppTutorial
Both strings are equal.
String 3: JavaCppTutorial
String 4: Javacpptutorial
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
int compare (const string &str) const;
Let's develop a basic program to contrast two strings by utilizing the compare method in C++.
Program2.cpp
#include <iostream>
using namespace std;
int main ()
{
string str1, str2; // declare string variable
cout << " Enter the string 1: ";
cin >> str1;
cout << " Enter the string 2: ";
cin >> str2;
// use compare() function to compare the second string with first string
int i = str1.compare(str2);
if ( i < 0)
{
cout << str1 << " is smaller than " << str2 << " string" << endl;
}
else if ( i > 0)
{
cout << str2 << " is greater than " << str1 << " string." << endl;
}
else // i == 0;
{
cout << " Both strings are equal.";
}
return 0;
}
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 symbol employed in C++ to compare two strings or numeric values. In C++, various relational operators exist including '==', '!=', >, <. However, for string comparison, we primarily utilize two operators: '==' for equality and '!=' for inequality.
Syntax
String1 == string2 // here, we use double equal to operator
Or
String1 != string2 // here, we use not equal to operator
Compare two strings using the Equal to (==) operator in C++
The equal to (==) operator is utilized to verify the equivalence of the initial string with the second string.
Let's develop a program in C++ to compare strings by using the equality operator (==).
Program3.cpp
#include <iostream>
using namespace std;
int main ()
{
// declare string variables
string str1;
string str2;
cout << " Enter the String 1: " << endl;
cin >> str1;
cout << " Enter the String 2: " << endl;
cin >> str2;
// use '==' equal to operator to check the equality of the string
if ( str1 == str2)
{
cout << " String is equal." << endl;
}
else
{
cout << " String is not equal." << endl;
}
return 0;
}
Output
Enter the String 1:
JavaCppTutorial
Enter the String 2:
javacpptutorial
String is not equal.
2 nd 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 develop a program in C++ that compares strings for inequality using the Not Equal To (!=) operator.
Program4.cpp
#include <iostream>
using namespace std;
int main ()
{
// declare string variables
string str1;
string str2;
cout << " Enter the String 1: " << endl;
cin >> str1;
cout << " Enter the String 2: " << endl;
cin >> str2;
// use '!=' not equal to operator to check the equality of the string
if ( str1 != str2)
{
cout << " String is not equal." << endl;
}
else
{
cout << " String is equal." << endl;
}
return 0;
}
Output
Enter the String 1:
JAVACppTutorial
Enter the String 2:
JavaCPPTUTORIAL
String is not equal.
2 nd 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
#include <iostream>
using namespace std;
int main ()
{
char s1[50], s2[50]; // declare character array
int i, disp;
cout << " Enter the String 1: " << endl;
cin >> s1;
cout << " Enter the String 2: " << endl;
cin >> s2;
for (i = 0; s1[i] == s2[i] && s1[i] == '\0'; i++);
if (s1[i] < s2[i])
{
cout << " String 1 is less than String 2";
}
else if (s1[i] > s2[i])
{
cout << " String 2 is less than String 1";
}
else
{
cout << " String 1 is equal to String 2";
}
return 0;
}
Output
Enter the String 1:
WELCOME
Enter the String 2:
WELCOME
String 1 is equal to String 2
Compare two strings using the User-defined function in C++
Let's develop a basic program to compare the initial string with a different string by employing a custom function in C++.
Program6.cpp
#include <iostream>
using namespace std;
void RelationalCompare ( string str1, string str2)
{
// use relational not equal operator
if ( str1 != str2)
{
cout << str1 << " is not equal to " << str2 << " string. " << endl;
if (str1 > str2)
{
cout << str1 << " is greater than " << str2 << " string." << endl;
}
else
{
cout << str2 << " is greater than " << str1 << " string." << endl;
}
}
else
cout << str1 << " is equal to " << str2 << " string." << endl;
}
int main ()
{
string str1 ( "JavaT");
string str2 ( "CppTutorial");
// call function
RelationalCompare (str1, str2);
string str3 ("JavaCppTutorial");
string str4 ("JavaCppTutorial");
RelationalCompare (str3, str4);
return 0;
}
Output
JavaT is not equal to CppTutorial string.
CppTutorial is greater than JavaT string.
JavaCppTutorial is equal to JavaCppTutorial string.