//using namespace
#include <string>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::getline;
//example_10
#include <cctype>
using std::isupper; using std::toupper;
using std::islower; using std::tolower;
using std::isalpha; using std::isspace;
// Note: #include and using declarations must be added to compile this code
int main()
{
//---example_1---
cout<<"Example_1:"<<endl;
string s0; // empty string
cin >> s0; // read whitespace-separated string into s
cout << s0<< endl;// write s to the output
//---example_2---
cout<<"Example_2:"<<endl;
cout << "Enter two numbers:" << endl;
int v1, v2;
cin >> v1 >> v2;
cout << "The sum of " << v1
<< " and " << v2
<< " is " << v1 + v2 << endl;
//---example_3---
cout<<"Example_3:"<<endl;
string s1, s2;
cin >> s1 >> s2; // read first input into s1, second into s2
cout << s1 << s2 << endl; // write both strings
/*
//---example_4---
cout<<"Example_4:"<<endl;
string word;
// read until end-of-file, writing each word to a new line
while (cin >> word)
cout << word << endl;
//---example_5---
cout<<"Example_5:"<<endl;
string line;
// read line at time until end-of-file
while (getline(cin, line))
cout << line << endl;
*/
//---example_6---
cout<<"Example_6:"<<endl;
string st11; // empty string
string st22(st11); // st22 is a copy of st11
string st33("Hello World"); // st33 holds Hello World
string st("The expense of spirit\n");
cout << "The size of " << st << "is " << st.size()
<< " characters, including the newline" << endl;
//---example_7---
cout<<"Example_7:"<<endl;
string substr = "Hello";
string phrase = "Hello World";
string slang = "Hiya";
if (substr < phrase)cout << "substr is smaller" << endl;
if (slang > substr) cout << "slang is greater" << endl;
if (slang > phrase) cout << "slang is greater" << endl;
//---example_8---
cout<<"Example_8:"<<endl;
string s111("hello, ");
string s222("world\n");
string s333 = s111 + s222; // s333 is hello, world\n
// print what we've got so far
cout << "s111: " << s111 << " s222: " << s222 << " s333: " << s333 << endl;
s111 += s222; // equivalent to s111 = s111 + s222
// print after update to s111
cout << "s111: " << s111 << " s222: " << s222 << " s333: " << s333 << endl;
{
// a better way to ``add'' punctuation
string s1_1("hello");
string s2_2("world");
string s3_3 = s1_1 + ", " + s2_2 + "\n";
// print again, now there won't be a newline after printing s2
cout << "s1_1: " << s1_1 << " s2_2: " << s2_2 << " s3_3: " << s3_3 << endl;
}
//---example_9---
cout<<"Example_9:"<<endl;
string str("John");
for (string::size_type ix = 0; ix != str.size(); ++ix)
cout << str[ix] << endl;
for (string::size_type ix = 0; ix != str.size(); ++ix)
str[ix] = '*';
cout << str << endl;
//---example_10---
cout<<"Example_10:"<<endl;
string s("Hello World!!!");
string::size_type punct_cnt = 0;
// count number of punctuation characters in s
for (string::size_type index = 0; index != s.size(); ++index)
if (ispunct(s[index]))//判断s当前字符是否为标点
++punct_cnt;
cout << punct_cnt
<< " punctuation characters in " << s << endl;
{
// convert s to lowercase
for (string::size_type index = 0; index != s.size(); ++index)
s[index] = tolower(s[index]);
cout << s << endl;
}
return 0;
}