Sitemap
Rohan Rai

Rohan Rai

Technical writer and Tech Blogger

Comparing strings in C++

4 min readMar 13, 2022

--

Press enter or click to view image in full size
Source

Abstract

Comparing 2 strings nowadays is becoming a common phenomenon in competitive programming. Usually, string comparison algorithms compare strings character by character. However, C++ provides some in-built operators like == or != along with inbuilt functions like strcmp or compare

Scope of article

  • This article provides different ways to compare strings in C++

Prerequisite

  • One should have a basic understanding of C++
  • The idea about string and string comparison algorithms

Introduction

Most of us while typing an email address or typing a password have observed that the system recognizes the password and if correctly typed it allow you access. One may wonder how the system performs such an operation.

Here system uses the algorithm of string comparison where it compares the original password stored with the password typed by the user. If both strings equally match with each other, then

Voila !!!

Before we dive into understanding different methods present in C++ for comparison of 2 strings, Let us see different scenarios of string comparison with the help of some examples:

From above, it is visible that for case 1, given strings are not equal to each other, while for case 2, given strings are equal to each other

Different methods of comparing strings

Here we going to see various methods in C++, through which given 2 strings can be compared.

  1. Using relational operators for comparing strings

Most of us have the idea that relational operators in C++ like ‘==’ are used to compare two entities like integers or float variables.

There are 2 kinds of the relational operator that we wish to highlight

‘==’ denotes equals to the relational operator

‘!=’ denotes not equals to relation operator

Output

Here it can be observed that since the given strings are not equal to each other i.e string a is “Scaler” and string b is “codeforce”. Therefore output would be that Scaler is not equal to codeforce.

2. Using compare function for comparing strings

Get Rohan Rai’s stories in your inbox

Join Medium for free to get updates from this writer.

It is a defined function in the standard library of C++ to compare 2 strings.

While comparing 2 strings it produces 2 types of outputs

  • Equal to 0
    When given two strings are lexicographically equal
  • Not equal to 0
    >
    If the value of compare output is greater than 0 (> 0) then the first string is lexicographically greater than the second string
    > If the value of compare output is less than 0 (< 0), then the first string is lexicographically smaller than the second string.

Let’s understand compare function with the help of an example

Here it can be observed that for example 1, string b (“targeted”) is greater than string a (“target”)

For example 2, the output will be Both strings are equal.

Before we jump and look into 3rd method of string comparison, let us first look at some of the differences between relational operator and compare():

  • compare() function returns an integer value while relational operator returns boolean value
  • It is easier to compare substring in compare() while it requires a long procedure where each word is compared with that of another string in relational operator.

3. Using strcmp() function in C++ for comparing strings

strcmp() is an inbuilt function available in C++ used to lexicographically compare two strings.

While comparing 2 strings, strcmp() produces 2 kinds of output

  • Equal to 0
    When the given strings are lexicographically equal to each other
  • Not equal to 0
    >
    If the output of the strcmp function comes out to be greater than 0 , then string 1 is greater than string 2.
    > If the output of the strcmp function comes out to be less than 0, then string 2 is greater than string 1.

Here it can be observed that for example 1, string b (“targeted”) is greater than string a (“target”)

For example 2, the output will be Both strings are equal.

For example 3, string f(“Metallica”) is greater than string e(“Megadeth”)

However, the issue with strcmp() function is that it compares both strings to the null characters. Thus if strings passed in the argument of strcmp() are not terminated with the null character, the comparison continues till the system crashes.

To avoid this situation, strncmp() is used. In its functioning, it is more or less similar to strcmp() but it takes another argument as integer highlighting the maximum number of characters to be compared

Here it can be observed that since the number of characters to be compared is 6, thus the output of the program will be Both strings are equal.

Conclusion

In this article, we tried to understand the basics of string comparison and different methods which can be used to compare 2 strings i.e. using the relational operator and some inbuilt functions of C++ like compare(), strcmp ().

To further enhance your knowledge on understanding the basic algorithm of string comparison, the intuition behind how it works, and its usage in various competitive platforms like CodeChef with beautiful schematics, one may want to have a look at this curated article published on the Comparing Two Strings in C++ on Scaler topics.

--

--

Rohan Rai

Written by Rohan Rai

Technical writer and Tech Blogger