第五章—语句

本文提供了C++ Primer第五版中部分习题的解答代码,包括第5章的两个练习题,通过比较输入字符串来找出重复单词,并针对大小写进行了区分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于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;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值