#include #include #include //for class string using namespace std; int main() { string s = "Hello there"; //one-argument constructor cout << "s.size() == " << s.size() << "\n" //instead of strlen << "The 1st character is '" << s[0] << "'.\n" //s.operator[](0) << "The next 3 characters are \"" << s.substr(1, 3) << "\".\n" << "The last character is '" << s[s.size() - 1] << "'.\n\n"; cout << "Please type your name and press RETURN: "; string word; //No-argument constructor puts null string into object. cin >> word; //Input 1 word like scanf(%s; string expands to hold it. string line = s + ", " + word + "! "; line += "How are you?"; //instead of strcat: line.operator+=("How RU?"); cout << line << "\n\n"; if (s < line) { //instead of strcmp: if (operator<(s, line)) { line = s; //instead of strcpy: line.operator=(s); } for (string::const_iterator it = s.begin(); it != s.end(); ++it) { cout << *it; } cout << "\n\n"; string::size_type i = s.find('l'); //instead of strchr if (i == string::npos) { //"no position" cout << "The string \"" << s << "\" does not contain 'l'.\n"; } else { cout << "Found the first 'l' at position " << i << ".\n"; } i = s.find("lo"); //instead of strstr if (i == string::npos) { //"no position" cout << "The string \"" << s << "\" does not contain \"lo\".\n"; } else { cout << "Found the first \"lo\" at position " << i << ".\n"; } //char *p = s; //won't compile //char *p = s.c_str(); //won't compile const char *p = s.c_str(); //will compile: pointer must be read-only s[0] = '\0'; return EXIT_SUCCESS; }