第五章—语句

由于C++ primer第五版采用的c++11新标准,手头没有支持c++11的编译器,推荐大家用Ideone,支持c++14并且可以输入输出。从今天开始整理自己做的课后习题,网上答案很多,但自己写的也是一种不一样的思路。

//exercise 5.20
#include <iostream>
#include <string>
using namespace std;

int main() 
{
	string pre_str, cur_str;
	while(cin >> cur_str)
		{
			if(cur_str == pre_str)
				{
					cout << "The copy word is " << cur_str << endl;
					break;
				}
			pre_str = cur_str;
		}
	if(!cin)
		cout << "There is no any copy word." << endl;
	return 0;
}

//exercise 5.21
#include <iostream>
#include <string>
using namespace std;

int main() 
{
	string pre_str, cur_str;
	while(cin >> cur_str)
		{
			if(cur_str == pre_str)
				{
					if(cur_str[0] >= 'A' && cur_str[0] <= 'Z')
					{
						cout << "The copy word is " << cur_str << endl;
						break;
					}
					else
						continue;
				}
			pre_str = cur_str;
		}
	if(!cin)
		cout << "There is no any copy word." << endl;
	return 0;
}





你可能感兴趣的:(第五章—语句)