#include <iostream>
#include <string>
int main()
{
std::string text1("Cat");
std::string text2 = "Dolphin"; // alternative declaration
std::cout << "The string \"" << text1 << "\" contains " << text1.length() << " chars.\n";
if (text1 != text2) // can directly compare
std::cout << '"' << text1 << "\" and \"" << text2 << "\" are not the same.\n";
else
std::cout << '"' << text1 << "\" and \"" << text2 << "\" are the same!\n";
std::string reversed_text2(text2.rbegin(), text2.rend());
std::cout << "The reverse of \"" << text2 << "\" is \"" << reversed_text2 << "\".\n";
// can easily build new strings by concatenating existing ones
// no worry about how many chars can text3 hold (it is dynamic)
std::string text3 = text1 + " and " + text2 + " are not good friends.";
std::cout << text3 << '\n';
std::cout << "The above text contains " << text3.length() << " chars.\n";
std::cout << std::endl;
}
The string "Cat" contains 3 chars.
"Cat" and "Dolphin" are not the same.
The reverse of "Dolphin" is "nihploD".
Cat and Dolphin are not good friends.
The above text contains 37 chars.