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:
strcmp() functionThe 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 JavaTpoint String 2: Welcome to JavaTpoint Both strings are equal. String 3: JavaTpoint String 4: Javatpoint The strings are not equal. compare() functionThe 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:
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 OperatorIt 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: JavaTpoint Enter the String 2: javatpoint 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 OperatorLet'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: JAVATpoint Enter the String 2: JavaTPOINT 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 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 create a simple program to compare the first string with another string using the user-defined function in C++. Program6.cpp Output JavaT is not equal to Tpoint string. Tpoint is greater than JavaT string. JavaTpoint is equal to JavaTpoint string.
Next TopicReverse an Array in C++
|