Home > Backend Development > C++ > body text

C++ program to compare the lexicographic order of two strings

PHPz
Release: 2023-09-04 17:13:06
forward
1517 people have browsed it

C++ program to compare the lexicographic order of two strings

Dictionary string comparison means that strings are compared in dictionary order. For example, if there are two strings 'apple' and 'appeal', the first string will come last because the first three characters of 'app' are the same. Then for the first string the character is 'l' and in the second string the fourth character is 'e'. Since 'e' is shorter than 'l', it will come first if we sort lexicographically.

Strings are compared lexicographically before arranging. In this article we will see Different techniques for lexicographically comparing two strings using C.

Using the compare() function in C strings

C string object has a compare() function, which accepts another string as input and compares it.

Compares the current string to the second string. This function will return 0 when two strings are the same When the strings are the same, it will return a negative number (-1) when the first string is larger When the first string is smaller, translate it to Chinese:

When the first string is smaller, it is a positive number (1).

grammar

<first string>.compare( <second string> )
Copy after login

Let's take a look at the algorithm and corresponding implementation in C.

algorithm

  • Take two strings s and t as input
  • cmp := Use the s.compare() function with the parameter t
  • If cmp equals 0, then
    • These two are the same
  • Otherwise, when cmp is positive, then
    • s is larger than t
  • Otherwise, when cmp is a negative number, 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;
}
Copy after login

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
Copy after login
Copy after login
Copy after login

Using the strcmp() function in C-style strings

In C, we can also use traditional C functions. C uses character arrays instead of string types.

data. To compare two strings the strcmp() functions are used. This function takes two Take a string as parameter. Returns 0 when they are the same. Returns a positive value when the first string is less than the second string One is when the second value is larger, it is the larger and negative value.

grammar

strcmp( <first string>, <second string> )
Copy after login
Copy after login

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;
}
Copy after login

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
Copy after login
Copy after login
Copy after login

Use comparison operators

Like numeric data, strings can also be compared using comparison operators. if-else conditions can be used directly for strings in C .

grammar

strcmp( <first string>, <second string> )
Copy after login
Copy after login

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;
}
Copy after login

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
Copy after login
Copy after login
Copy after login

in conclusion

String comparison is an important task that we perform in multiple applications. In C, There are several different ways to compare strings. The first is to use the compare() method The content that needs to be translated is: Which takes one string as input and checks with the current string. In C the comparison Operators such as (==), (>), (=) can be used for string comparison. on the other hand, C-like strings can be compared using the strcmp() function. This function accepts constants character pointers. The compare() method and the strcmp() method returns 0 when both When the first string is larger, a positive number is returned; when the two strings are the same, 0 is returned. The first one is smaller, it will return a positive number.

The above is the detailed content of C++ program to compare the lexicographic order of two strings. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template