// Rick Mercer 5-Oct-95 // // Demonstrate the string class as defined in the draft ANSI document as: // // typedef basic_string string; // #include // Borland's 4.5 cstring works (except for empty) #include #include void main() { string s1, s2, s3; s1 = "First"; s2 = "Second"; s3 = "Third"; // length returns the number of characters currently stored in the object cout << s1.length() << endl; assert (s1.length() == 5); /* BORLAND DOES NOT IMPLEMENT empty // Make s3 empty with assignment of the null string s3 = ""; // assert: s3 is a null string if(s3.empty()) cout << s3.length() << endl; // assert: s3 is empty. s3 stores the null string (s3's length == 0). TODO: Remove comment */ s3 = "A new string"; // the subscript operator [ ] returns individual characters, // with [0] referencing the first character cout << "s3[4] == " << s3[4] << endl; // assert: s3[4] == 'w' // Change s1 and s2 for use in the next several examples s1 = "abcDEFghiHKLabcDEF"; s2 = "DEF"; // find the position of the occurrence of a string (s2) in another (s1) // If the string argument is not found, find returns a negative number int pos1 = s1.find(s2); // Assert: pos1 == 3 int pos2 = s1.find("Not There"); // Assert: pos2 < 0 // Find the first occurrence of a character in a string (s1) int pos3 = s1.find('K'); // Assert: pos3 == 10 cout << "\nPositions" << endl; cout << "pos1: " << pos1 << endl; cout << "pos2: " << pos2 << endl; cout << "pos3: " << pos3 << endl; }