Comparing two strings in C++ - GeeksforGeeks (2024)

Table of Contents
CPP CPP

Given two strings, how to check if the two strings are equal or not.
Examples:

Input : ABCD, XYZOutput : ABCD is not equal to XYZ XYZ is greater than ABCDInput : Geeks, forGeeksOutput : Geeks is not equal to forGeeks forGeeks is greater than Geeks

This problem can be solved using any of the following two methods

  • C++ Relational operators

CPP

// CPP code to implement relational

// operators on string objects

#include <iostream>

using namespace std;

void relationalOperation(string s1, string s2)

{

if (s1 != s2)

{

cout << s1 << " is not equal to " << s2 << endl;

if (s1 > s2)

cout << s1 << " is greater than " << s2 << endl;

else

cout << s2 << " is greater than " << s1 << endl;

}

else

cout << s1 << " is equal to " << s2 << endl;

}

// Driver code

int main()

{

string s1("Geeks");

string s2("forGeeks");

relationalOperation(s1, s2);

string s3("Geeks");

string s4("Geeks");

relationalOperation(s3, s4);

return 0;

}

Output

Geeks is not equal to forGeeksforGeeks is greater than GeeksGeeks is equal to Geeks

Time Complexity: O(min(n,m)) where n and m are the length of the strings.

Auxiliary Space: O(max(n,m)) where n and m are the length of the strings.

This is because when string is passed in the function it creates a copy of itself in stack.

  • std:: Compare()

CPP

// CPP code perform relational

// operation using compare function

#include <iostream>

using namespace std;

void compareFunction(string s1, string s2)

{

// comparing both using inbuilt function

int x = s1.compare(s2);

if (x != 0) {

cout << s1

<< " is not equal to "

<< s2 << endl;

if (x > 0)

cout << s1

<< " is greater than "

<< s2 << endl;

else

cout << s2

<< " is greater than "

<< s1 << endl;

}

else

cout << s1 << " is equal to " << s2 << endl;

}

// Driver Code

int main()

{

string s1("Geeks");

string s2("forGeeks");

compareFunction(s1, s2);

string s3("Geeks");

string s4("Geeks");

compareFunction(s3, s4);

return 0;

}

Output

Geeks is not equal to forGeeksforGeeks is greater than GeeksGeeks is equal to Geeks

Time Complexity: O(min(n,m)) where n and m are the length of the strings.

Auxiliary Space: O(max(n,m)) where n and m are the length of the strings.

This is because when string is passed in the function it creates a copy of itself in stack.

Differences between C++ Relational operators and compare() :-

  1. compare() returns an int, while relational operators return boolean value i.e. either true or false.
  2. A single Relational operator is unique to a certain operation, while compare() can perform lots of different operations alone, based on the type of arguments passed.
  3. We can compare any substring at any position in a given string using compare(), which otherwise requires the long procedure of word-by-word extraction of string for comparison using relational operators.

Example:-

  • Using compare()
// Compare 3 characters from 3rd position// (or index 2) of str1 with 3 characters // from 4th position of str2. if (str1.compare(2, 3, str2, 3, 3) == 0) cout<<"Equal";else cout<<"Not equal";
  • Using Relational operator
for (i = 2, j = 3; i <= 5 && j <= 6; i++, j++) { if (s1[i] != s2[j]) break;}if (i == 6 && j == 7) cout << "Equal";else cout << "Not equal";

The above example clearly shows how compare() reduces lots of extra processing, therefore it is advisable to use it while performing substring comparison at some position, otherwise both perform almost in the same manner.


Last Updated : 23 Jun, 2022

Like Article

Save Article

I am a seasoned programming enthusiast with a strong command of C++ and string manipulation. My expertise lies in algorithmic problem-solving, particularly in string comparison and manipulation in C++.

The problem you've described involves checking the equality of two strings and determining their relative ordering, which can be achieved through various methods in C++. The article discusses two approaches: one utilizing C++ relational operators and another employing the compare() function for string comparison.

In the provided code snippets, the first method uses C++ relational operators (==, !=, >, <) to compare strings by directly applying these operators on string objects. It checks for equality and determines which string is lexicographically greater. This method involves creating copies of strings in the stack and has a time complexity of O(min(n,m)) and auxiliary space complexity of O(max(n,m)), where n and m are the lengths of the strings being compared.

The second method involves using the compare() function, which returns an integer value instead of a boolean as in relational operators. This function performs a comparison and indicates whether the strings are equal or which one is greater. Similar to the first method, it also requires copying strings and has the same time and space complexities.

Differences between the two approaches are highlighted:

  1. Return Values: Relational operators return boolean values, while compare() returns an integer (-1, 0, 1) indicating less than, equal to, or greater than respectively.
  2. Function Versatility: compare() can perform various comparison operations based on the arguments passed, while each relational operator is specific to its operation.
  3. Substring Comparison: compare() allows easy comparison of substrings at any position in a string without requiring extensive manual extraction, offering a more concise approach.

The article also provides examples showcasing how compare() simplifies substring comparisons compared to using relational operators, especially in terms of reducing unnecessary processing.

To summarize, both methods accomplish string comparison tasks efficiently, but compare() offers greater versatility and ease when dealing with substrings and specific positions within strings, making it a favorable choice for such operations.

Comparing two strings in C++ - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Nathanial Hackett

Last Updated:

Views: 6077

Rating: 4.1 / 5 (72 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Nathanial Hackett

Birthday: 1997-10-09

Address: Apt. 935 264 Abshire Canyon, South Nerissachester, NM 01800

Phone: +9752624861224

Job: Forward Technology Assistant

Hobby: Listening to music, Shopping, Vacation, Baton twirling, Flower arranging, Blacksmithing, Do it yourself

Introduction: My name is Nathanial Hackett, I am a lovely, curious, smiling, lively, thoughtful, courageous, lively person who loves writing and wants to share my knowledge and understanding with you.