// Demonstrate the string class as defined in the draft ANSI document as: // // typedef basic_string string; // // Each string object is an ordered sequence of characters. // // 6/26/95 The group that was in the Colgate PC lab for OO Workshop // // Modified by Rick Mercer on 25-Sep-95 and 5-Oct-95 // Note: I used Borland's string class as defined in // The only difference I found was Borland lacks string::empty() // It would be simple for me to modify an existing class to // implement the subset. // // Deno preview // 1. Declare // 2. ... // 3. I/O // #include // Borland's 4.5 cstring works (except for empty) #include main() { //-----constructors string s1; // assert: s1 has a length of 0 (the null, or emtpy, string) string s2("Initializer constructor"); // assert: s2 == "Initializer constructor" (s2's length == 11) string s3(s2); // assert: s3's characters and length are an exact copy of s2 cout << "Initialized values: " << endl; cout << "s1: " << s1 << endl; cout << "s2: " << s2 << endl; cout << "s3: " << s3 << endl; //-----assignments s1 = s2; // assert: s1 == s2; s1 = "Hi"; // assert: s1's characters are "Hi" and s1's length is 2 s2 = 'A'; // assert: s2's has length of 1 and s2[0] == 'A' cout << "\nNew values: " << endl; cout << "s1: " << s1 << endl; cout << "s2: " << s2 << endl; //-----stream I/O functions (>>, <<, and getline) // String input data is terminated like the other data, // by spaces, tabs, and new lines. cout << "Enter a string [no blanks!]: "; cin >> s1; cout << "You entered " << s1 << endl; // assert: Except for leading blanks, tabs, and newlines (terminators), // all characters up to the first terminator are stored in s1 // Use getline to extract entire lines that may have blanks and tabs cout << "Enter a line of input with blanks, press enter to stop:\n"; getline(cin, s1); cout << "\nYou entered: " << s1 << endl; // assert: If the input was one line with the input // An entire line (string with blanks) // then s1 == "An entire line (string with blanks)" }