// // Program to demonstrate use of lstring class. // // Author: B. Massingill. // // Input: Text strings, two at a time; control-D to end. // (Program prompts for these.) // Output: Results of applying various string operations // to the input strings, using the lstring class. // #include #include // has EXIT_SUCCESS #include // has toupper() #include "lstring.h" int main(void) { // Several ways to declare and initialize lstrings. lstring empty; // empty lstring. lstring one_char('a'); // lstring built from one char. lstring some_chars("abcd"); // lstring built from C-style string. // Stream output works on lstrings. cout << "Empty string = " << empty << endl; cout << "One-character string = " << one_char << endl; cout << "Multi-character string = " << some_chars << endl; // empty() member function returns true if empty, false otherwise. cout << "Is empty string empty? "; if (empty.empty()) cout << "yes\n"; else cout << "no\n"; lstring s1, s2; char prompt[] = "Enter two strings s1 and s2, or control-D to end:\n"; cout << prompt; // Stream input works on lstrings. while (cin >> s1 >> s2) { cout << "You entered: " << s1 << " and " << s2 << endl; // s.empty() is true if s is empty, false otherwise. if (s1.empty()) cout << "s1 is empty\n"; if (s2.empty()) cout << "s2 is empty\n"; // s.length() is the length of s. cout << "Length of s1 is " << s1.length() << endl; cout << "Length of s2 is " << s2.length() << endl; // Relational operators work on lstrings (lexicographic // comparison, as with C++ strings). if (s1 == s2) cout << "s1 == s2\n"; if (s1 != s2) cout << "s1 != s2\n"; if (s1 <= s2) cout << "s1 <= s2\n"; if (s1 >= s2) cout << "s1 >= s2\n"; if (s1 < s2) cout << "s1 < s2\n"; if (s1 > s2) cout << "s1 > s2\n"; // s.find(t) is true if t is found in s (i.e., t is a // substring of s), false otherwise. if (s1.find(s2)) cout << "s2 found in s1\n"; if (s2.find(s1)) cout << "s1 found in s2\n"; // s.getElementAt(i) is the i-th character of s. // lstring::length_pos is the data type used in lstring // for lengths and positions. for (lstring::length_pos i = 0; i < s1.length(); ++i) cout << "element " << i << " of s1 is " << s1.getElementAt(i) << endl; // s.replaceElementAt(j, c) replaces the j-th character of s // with c. // here we use it to translate an lstring to all uppercase. for (lstring::length_pos j = 0; j < s2.length(); ++j) s2.replaceElementAt(j, static_cast(toupper(s2.getElementAt(j)))); cout << "s2 in upper case is " << s2 << endl; // "+" for lstrings is concatenation. cout << "s1 plus s2 is " << s1 + s2 << endl; // s.append(t) appends t to s. cout << "appending 'abcd' to s1....\n"; s1.append(empty); s1.append(some_chars); cout << "result is " << s1 << endl; // s.insertBefore(t, i) inserts t into s before the i-th // character. (If i is s.length(), this is the same as // appending t to s.) cout << "inserting empty string into s2 several times....\n"; s2.insertBefore(empty, 0); s2.insertBefore(empty, s2.length()); s2.insertBefore(empty, 1); cout << "result is " << s2 << endl; cout << "inserting 'abcd' at start of s2....\n"; s2.insertBefore(some_chars, 0); cout << "result is " << s2 << endl; cout << "inserting 'abcd' at end of s2....\n"; s2.insertBefore(some_chars, s2.length()); cout << "result is " << s2 << endl; cout << "inserting 'abcd' into s2 before character " << s2.length()/2 << "....\n"; s2.insertBefore(some_chars, s2.length()/2); cout << "result is " << s2 << endl; cout << prompt; } return EXIT_SUCCESS; }