string 类应用

//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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Digital2Slave

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值