21.3 Comparing strings

Class string provides member functions for comparing strings. Figure 21.2 demonstrates class string’s comparison capabilities.

Fig. 21.2 Comparing strings.

Alternate View

 1  // Fig. 21.2: Fig21_02.cpp
 2  // Comparing strings.
 3  #include <iostream>
 4  #include <string>
 5  using namespace std;
 6
 7  int main() {
 8     string string1{"Testing the comparison functions."};
 9     string string2{"Hello"};
10     string string3{"stinger"};
11     string string4{string2}; // "Hello"
12
13     cout << "string1: " << string1 << "
string2: " << string2
14        << "
string3: " << string3 << "
string4: " << string4 << "

";
15
16     // comparing string1 and string4
17     if (string1 == string4) {
18        cout << "string1 == string4
";
19     }
20     else if (string1 > string4) {
21     cout << "string1 > string4
";
22     }
23     else { // string1 < string4
24        cout << "string1 < string4
";
25     }
26
27     // comparing string1 and string2
28     int result{string1.compare(string2)};
29
30     if (result == 0) {
31        cout << "string1.compare(string2) == 0
";
32     }
33     else if (result > 0) {
34        cout << "string1.compare(string2) > 0
";
35     }
36     else { // result < 0
37        cout << "string1.compare(string2) < 0
";
38     }
39
40     // comparing string1 (elements 2-5) and string3 (elements 0-5)
41     result = string1.compare(2, 5, string3, 0, 5);
42
43     if (result == 0) {
44        cout << "string1.compare(2, 5, string3, 0, 5) == 0
";
45     }
46     else if (result > 0) {
47        cout << "string1.compare(2, 5, string3, 0, 5) > 0
";
48     }
49     else { // result < 0
50        cout << "string1.compare(2, 5, string3, 0, 5) < 0
";
51     }
52
53     // comparing string2 and string4
54     result = string4.compare(0, string2.size(), string2);
55
56     if (result == 0) {
57        cout << "string4.compare(0, string2.size(), string2) == 0
";
58     }
59     else if (result > 0) {
60        cout << "string4.compare(0, string2.size(), string2) > 0
";
61     }
62     else { // result < 0
63        cout << "string4.compare(0, string2.size(), string2) < 0
";
64     }
65
66     // comparing string2 and string4
67     result = string2.compare(0, 3, string4);
68
69     if (result == 0) {
70        cout << "string2.compare(0, 3, string4) == 0
";
71     }
72     else if (result > 0) {
73        cout << "string2.compare(0, 3, string4) > 0
";
74     }
75     else { // result < 0
76        cout << "string2.compare(0, 3, string4) < 0
";
77     }
78  }

string1: Testing the comparison functions.
string2: Hello
string3: stinger
string4: Hello

string1 > string4
string1.compare(string2) > 0
string1.compare(2, 5, string3, 0, 5) == 0
string4.compare(0, string2.size(), string2) == 0
string2.compare(0, 3, string4) < 0

The program declares four strings (lines 8–11) and outputs each (lines 13–14). Line 17 tests string1 against string4 for equality using the overloaded equality operator. If the condition is true, "string1 == string4" is output. If the condition is false, the condition in line 20 is tested. All the string class overloaded relational and equality operator functions return bool values.

Line 28 uses string member function compare to compare string1 to string2. Variable result is assigned 0 if the strings are equivalent, a positive number if string1 is lexicographically greater than string2 or a negative number if string1 is lexicographically less than string2. When we say that a string is lexicographically less than another, we mean that the compare member function uses the numerical values of the characters (see Appendix B, ASCII Character Set) in each string to determine that the first string is less than the second. A lexicon is a dictionary. Because a string starting with 'T' is considered lexicographically greater than a string starting with 'H', result is assigned a value greater than 0, as confirmed by the output.

Line 41 compares portions of string1 and string3 using an overloaded version of member function compare. The first two arguments (2 and 5) specify the starting subscript and length of the portion of string1 ("sting") to compare with string3. The third argument is the comparison string. The last two arguments (0 and 5) are the starting subscript and length of the portion of the comparison string being compared (also "sting"). The value assigned to result is 0 for equality, a positive number if string1 is lexicographically greater than string3 or a negative number if string1 is lexicographically less than string3. The two pieces being compared here are identical, so result is assigned 0.

Line 54 uses another overloaded version of function compare to compare string4 and string2. The first two arguments are the same—the starting subscript and length. The last argument is the comparison string. The value returned is also the same—0 for equality, a positive number if string4 is lexicographically greater than string2 or a negative number if string4 is lexicographically less than string2. Because the two pieces of strings being compared here are identical, result is assigned 0.

Line 67 calls member function compare to compare the first 3 characters in string2 to string4. Because "Hel" is less than "Hello", a value less than zero is returned.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset