Comparing strings

Class string provides member functions for comparing strings. Figure 18.2 demonstrates class string's comparison capabilities.

Figure 18.2. Comparing strings.

(This item is displayed on pages 888 - 889 in the print version)

 1 // Fig. 18.2: Fig18_02.cpp
 2 // Demonstrating string comparison capabilities.
 3 #include 
 4 using std::cout;
 5 using std::endl;
 6
 7 #include 
 8 using std::string;
 9
10 int main()
11 {
12 string string1( "Testing the comparison functions." );
13 string string2( "Hello" );
14 string string3( "stinger" );
15 string string4( string2 );
16
17 cout << "string1: " << string1 << "
string2: " << string2
18 << "
string3: " << string3 << "
string4: " << string4 << "

";
19
20 // comparing string1 and string4
21 if ( string1 == string4 )
22 cout << "string1 == string4
";
23 else // string1 != string4
24 {
25 if ( string1 > string4 )
26 cout << "string1 > string4
";
27 else // string1 < string4
28 cout << "string1 < string4
";
29 } // end else
30
31 // comparing string1 and string2
32 int result = string1.compare( string2 );
33
34 if ( result == 0 )
35 cout << "string1.compare( string2 ) == 0
";
36 else // result != 0
37 {
38 if ( result > 0 )
39 cout << "string1.compare( string2 ) > 0
";
40 else // result < 0
41 cout << "string1.compare( string2 ) < 0
";
42 } // end else
43
44 // comparing string1 (elements 2-5) and string3 (elements 0-5)
45 result = string1.compare( 2, 5, string3, 0, 5 );
46
47 if ( result == 0 )
48 cout << "string1.compare( 2, 5, string3, 0, 5 ) == 0
";
49 else // result != 0
50 {
51 if ( result > 0 )
52 cout << "string1.compare( 2, 5, string3, 0, 5 ) > 0
";
53 else // result < 0
54 cout << "string1.compare( 2, 5, string3, 0, 5 ) < 0
";
55 } // end else
56
57 // comparing string2 and string4
58 result = string4.compare( 0, string2.length(), string2 );
59
60 if ( result == 0 )
61 cout << "string4.compare( 0, string2.length(), "
62 << "string2 ) == 0" << endl;
63 else // result != 0
64 {
65 if ( result > 0 )
66 cout << "string4.compare( 0, string2.length(), "
67 << "string2 ) > 0" << endl;
68 else // result < 0
69 cout << "string4.compare( 0, string2.length(), "
70 << "string2 ) < 0" << endl;
71 } // end else
72
73 // comparing string2 and string4
74 result = string2.compare( 0, 3, string4 );
75
76 if ( result == 0 )
77 cout << "string2.compare( 0, 3, string4 ) == 0" << endl;
78 else // result != 0
79 {
80 if ( result > 0 )
81 cout << "string2.compare( 0, 3, string4 ) > 0" << endl;
82 else // result < 0
83 cout << "string2.compare( 0, 3, string4 ) < 0" << endl;
84 } // end else
85
86 return 0;
87 } // end main
 
 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.length(), string2 ) == 0
 string2.compare( 0, 3, string4 ) < 0
 

The program declares four strings with lines 1215 and outputs each string (lines 1718). The condition in line 21 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 25 is tested. All the string class overloaded operator functions demonstrated here as well as those not demonstrated here (!=, <, >= and <=) return bool values.



Line 32 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. 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. A lexicon is a dictionary. When we say that a string is lexicographically less than another, we mean that the first string is alphabetically less than the second. The computer uses the same criterion as you would use in alphabetizing a list of names.


Line 45 uses an overloaded version of member function compare to compare portions of string1 and string3. 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. Because the two pieces of strings being compared here are identical, result is assigned 0.

Line 58 uses another overloaded version of function compare to compare string4 and string2. The first two arguments are the samethe starting subscript and length. The last argument is the comparison string. The value returned is also the same0 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 74 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.

Introduction to Computers, the Internet and World Wide Web

Introduction to C++ Programming

Introduction to Classes and Objects

Control Statements: Part 1

Control Statements: Part 2

Functions and an Introduction to Recursion

Arrays and Vectors

Pointers and Pointer-Based Strings

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 2

Operator Overloading; String and Array Objects

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

Templates

Stream Input/Output

Exception Handling

File Processing

Class string and String Stream Processing

Web Programming

Searching and Sorting

Data Structures

Bits, Characters, C-Strings and structs

Standard Template Library (STL)

Other Topics

Appendix A. Operator Precedence and Associativity Chart

Appendix B. ASCII Character Set

Appendix C. Fundamental Types

Appendix D. Number Systems

Appendix E. C Legacy Code Topics

Appendix F. Preprocessor

Appendix G. ATM Case Study Code

Appendix H. UML 2: Additional Diagram Types

Appendix I. C++ Internet and Web Resources

Appendix J. Introduction to XHTML

Appendix K. XHTML Special Characters

Appendix L. Using the Visual Studio .NET Debugger

Appendix M. Using the GNU C++ Debugger

Bibliography



C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net