
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Compare two strings lexicographically
Lexicographic string comparison states the strings are compared in dictionary order. For example, if two strings ‘apple’ and ‘appeal’ are there, the first string will come next because the first three characters ‘app’ is the same. Then for the first string, the character is ‘l’ and in the second string, the 4th character is ‘e’. Since ‘e’ is shorter than ‘l’, it will come first if we arrange them in lexicographic order.
Before arranging, the strings are compared lexicographically. In this article, we will see different techniques to compare two strings in a lexicographic manner using C++.
Using compare() functions in C++ string
C++ string objects have compare() function which takes another string as input and compares the current string with the second string. This function will return 0 when two strings are the same, it will return a negative number (-1) when the first string is larger, and a positive (+1) when the first string is smaller.
Syntax
<first string>.compare( <second string> )
Let us see the algorithm and corresponding implementation in C++.
Algorithm
- Take two strings s and t as input
- cmp := use s.compare() function with parameter t
- if cmp is 0, then
- these two are the same
- otherwise when cmp is positive, then
- s is larger than t
- otherwise when cmp is negative, then
- s is smaller than t
- end if
Example
#include <iostream> using namespace std; string solve( string s, string t ){ int ret; ret = s.compare( t ); if( ret == 0 ) { return s + " and " + t + " are the same"; } else if( ret > 0 ) { return s + " is larger than " + t; } else { return s + " is smaller than " + t; } } int main(){ string s = "apple"; string t = "appeal"; cout << "The result of comparison: " << solve( s, t ) << endl; s = "popular"; t = "popular"; cout << "The result of comparison: " << solve( s, t ) << endl; s = "Hello"; t = "hello"; cout << "The result of comparison: " << solve( s, t ) << endl; }
Output
The result of comparison: apple is larger than appeal The result of comparison: popular and popular are the same The result of comparison: Hello is smaller than hello
Using strcmp() functions in C-like strings
In C++, we can use traditional C functions as well. C uses a character array instead of stringtype data. To compare two strings the strcmp() functions are used. This function takes two strings as arguments. Then returns 0 when they are the same. Positive value when the first one is the larger and negative value when the second one is larger.
Syntax
strcmp( <first string>, <second string> )
Example
#include <iostream> #include <cstring> using namespace std; string solve( const char* s, const char* t ){ int ret; ret = strcmp( s, t ); if( ret == 0 ) { return string(s) + " and " + string(t) + " are the same"; } else if( ret > 0 ) { return string(s) + " is larger than " + string(t); } else { return string(s) + " is smaller than " + string(t); } } int main(){ string s = "apple"; string t = "appeal"; cout << "The result of comparison: " << solve( s.c_str() , t.c_str()) << endl; s = "popular"; t = "popular"; cout << "The result of comparison: " << solve( s.c_str() , t.c_str()) << endl; s = "Hello"; t = "hello"; cout << "The result of comparison: " << solve( s.c_str() , t.c_str()) << endl; }
Output
The result of comparison: apple is larger than appeal The result of comparison: popular and popular are the same The result of comparison: Hello is smaller than hello
Using comparison operators
Like numeric data, the strings can also be compared using comparison operators. The if-else conditions can be used directly for strings in C++.
Syntax
strcmp( <first string>, <second string> )
Example
#include <iostream> using namespace std; string solve( string s, string t ){ int ret; if( s == t ) { return s + " and " + t + " are the same"; } else if( s > t ) { return s + " is larger than " + t; } else { return s + " is smaller than " + t; } } int main(){ string s = "apple"; string t = "appeal"; cout << "The result of comparison: " << solve( s, t ) << endl; s = "popular"; t = "popular"; cout << "The result of comparison: " << solve( s, t ) << endl; s = "Hello"; t = "hello"; cout << "The result of comparison: " << solve( s, t ) << endl; }
Output
The result of comparison: apple is larger than appeal The result of comparison: popular and popular are the same The result of comparison: Hello is smaller than hello
Conclusion
String comparison is an important task that we perform in several applications. In C++ there are a few different methods to compare strings. The first one is using compare() method. Which takes one string as input and checks with the current string. In C++ the comparison operators like (==), (>), (<) (<=), (>=) can be used for string comparison. On the other hand, C-like strings can be compared using the strcmp() function. Which accepts constant character pointers. The compare() method and the strcmp() method returns 0 when both strings are the same, when the first one is larger, it returns a positive number and when the first one is smaller, it will return a positive number.
- Related Articles
- Java Program to Compare two strings lexicographically
- Golang program to compare two strings lexicographically
- Compare two strings lexicographically in Java.
- Compare two strings lexicographically in C#
- Compare two Strings lexicographically in Java programming
- Compare the two Strings lexicographically in Java
- Java Program to Compare Two Strings
- Golang program to compare two strings
- Program to Compare two strings in Java
- Golang Program to compare two strings by ignoring case
- Comparing two Strings lexicographically in Java \n
- Java Program to Compare Strings
- Compare two Strings in Java
- How to compare two strings in Golang?
- Write a C program to compare two strings using strncmp library function
