The string class supports the operators + and += for concatenating, and the operators ==, !=, <, <=, >, and >= for comparing strings.
You can use the + operator to concatenate strings, that is, to join those strings together.
string sum, s1("sun"), s2("flower"); sum = s2 + s3;
The code above concatenates the strings s1 and s2. The result, "sunflower" is then assigned to sum.
Two strings concatenated using the + operator will form an expression of the string type.
string s1("sun"),s2("flower"),s3("seed"); cout << s1 + s2 + s3; string s("Good morning "); cout << s + " test" + '!';
The following code reads several lines of text and outputs in reverse order.
#include <iostream> #include <string> using namespace std; string prompt("Please enter some text!\n"), line( 50, '-'); int main() //from www . j a v a 2s .c om { prompt+="Terminate the input with an empty line.\n "; cout << line << '\n' << prompt << line << endl; string text, line; // Empty strings while( true) { getline( cin, line); // Reads a line of text if( line.length() == 0) // Empty line? break; // Yes ->end of the loop text = line + '\n' + text; // Inserts a new // line at the beginning. } // Output: cout << line << '\n' << "Your lines of text in reverse order:" << '\n' << line << endl; cout << text << endl; return 0; }