| |
A string demonstration: assignment, concatenate, compare |
|
 |
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1("Alpha");
string str2("Beta");
string str3("Omega");
string str4;
str4 = str1;
cout << str1 << endl << str3 << endl;
str4 = str1 + str2; // concatenate two strings
cout << str4 << endl;
str4 = str1 + " to " + str3;
cout << str4 << endl;
if(str3 > str1) // compare strings
cout << "str3 > str1\n";
if(str3 == str1 + str2)
cout << "str3 == str1+str2\n";
str1 = "This is a null-terminated string.\n";
cout << str1;
// create a string object using another string object
string str5(str1);
cout << str5;
// input a string
cout << "Enter a string: ";
cin >> str5;
cout << str5;
return 0;
}
|
|
|
Related examples in the same category |
1. | String type class | |  | 2. | A filter to remove white-space characters at the ends of lines. | |  | 3. | Demonstrate insert(), erase(), and replace(). | |  | 4. | Use string: find, string::npos | |  | 5. | Strings: size, iterator, count, begin and end | |  | 6. | string: find( ) and rfind( ) | |  | 7. | Print a name in two different formats | |  | 8. | Several string operations: substr | |  | 9. | String Char Indexing | |  | 10. | String Find and replace | |  | 11. | String Size | |  | 12. | String SizeOf | |  | 13. | A short string demonstration | |  | 14. | String insert(), erase(), and replace() | |  | 15. | string variable instead of a character array | |  | 16. | Inputting Multiple Words into a String | |  | 17. | Adding Strings | |  | 18. | Read string from console | |  | 19. | Insert, search, and replace in strings. | |  | 20. | Accessing Characters In Strings | |  |
|