#include #include #include using std::string; using std::cin; using std::cout; using std::endl; using std::istringstream; using std::getline; int main(int argc, char **argv) { // initialize C++ string from C-string literal string s = "Hello world"; cout << s << endl; // can index into strings using []; // perform other operations via methods on the string cout << s[0] << s[s.length() - 1] << endl; // C++ strings do bounds checking, so this fails immediately // std::cout << s[-1] << std::endl; // can read from a string using a string stream just like cin string name = "Jim Glenn"; istringstream sin{name}; string first; sin >> first; // reading into a string stops at 1st whitespace cout << first << endl; // use getline function (not method) to read an entire line string line; cout << "Enter a line" << endl; getline(cin, line); cout << "You typed: " << line << endl; }