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.
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).
<first string>.compare( <second string> )
Let's take a look at the algorithm and corresponding implementation in C.
#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; }
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
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.strcmp( <first string>, <second string> )
#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; }
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
Like numeric data, strings can also be compared using comparison operators. if-else conditions can be used directly for strings in C .
strcmp( <first string>, <second string> )
#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; }
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
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!