Comparing Two Strings in C++

Video Tutorial
FREE
Introduction to Pattern Matching thumbnail
This video belongs to
String Pattern Matching: KMP Algorithm
1 modules
Certificate
Topics Covered

Overview

String comparison is a very common scenario in programming. Most string comparison algorithms compare the strings character by character. C++ provides us with built-in operators like == and != to compare the strings along with strcmp and compare functions. In this article, we will delve into three approaches for comparing strings in C++:

  1. Comparing two strings in C++ Using strcmp() Function in C++
  2. Comparing Two Strings in C++ Using compare() Function
  3. Comparing Two Strings in C++ Using C++ Relational Operators

1. Comparing two strings in C++ Using strcmp() Function

strcmp() is a C library function that lexicographically compares two strings.

Syntax:

The strcmp() function takes two strings (character arrays) as arguments and returns:

  • 0 if both strings are lexicographically equal.
  • Greater than 0 if firstString is lexicographically greater than secondString.
  • Less than 0 if firstString is lexicographically smaller than secondString.

In lexicographical order, if a word in a dictionary comes before another, the first word is considered lexicographically smaller. If they appear together in a dictionary, they are lexicographically equal.

Example:

Output:

For more information, read: strcmp() in C++

2. Comparing Two Strings in C++ Using compare() Function

compare() is a function defined in the standard library of C++ to compare two strings.

Syntax:

compare() function in C++ gives broadly two classes of outputs when comparing strings:

A. Class #1: equal to 0 ( == 0 )

Zero is returned when the strings being compared are lexicographically equal.

B. Class #2: not equal to 0 ( != 0 )

compare() function in C++ returns non-zero values when the strings being compared are lexicographically unequal

There are further two sub-classes of outputs for non-zero output returned by the compare() function:

  1. Greater than 0 ( > 0): this is returned when the first string is lexicographically greater than the second string
  2. Less than 0 ( < 0 ): this is returned when the first string is lexicographically smaller than the second string

Let’s understand this better using a code example in C++:

Output:

3. Comparing Two Strings in C++ Using C++ Relational Operators

Comparing Two Strings in C++ using C++ relational operators

Apart from the inbuilt functions of strcmp() & compare(), C++ also offers a vanilla way of comparing two strings using relational operators ( ==, != )

You might have observed that we used the ‘==’ relational operator many times above.

E.g., when we use equalOrNot == 0 in our code examples above, we are simply comparing whether the value of our equalOrNot variable is equal to 0.

Therefore, the ‘==’ operator is used to compare two entities (strings or integers, for instance) to see whether they are equal in value. It’s undoubtedly one of the easiest ways to compare two strings in C++. There are two kinds of relational operators we wish to highlight here:

A. ‘==’ relational operator or “equals to” relational operator

B. ‘!=’ relational operator or “not equals to” relational operator

Let’s further understand comparing strings using these relational operators via a code example in C++:

A. Using ‘==’ (equals to) relational operator in C++:

Output:

B. Using ‘!=’ (not equals to) relational operator in C++

Output:

Note how even if the way of writing the conditions in code changes, the outputs remain unchanged in the above programs (as “Scaler” and “Scaled” strings are indeed unequal).

For more information, read: What are Relational Operators in C++?

Conclusion

In this article, we learned three ways to compare strings in C++, using:

  • strcmp() inbuilt function
  • compare() inbuilt function, and
  • C++ relational operators ( ‘==’, ‘!=’)