C++Primer第五版 练习6.17

本文详细解读了C++ Primer第五版中的练习6.17,通过代码示例和注释提供了清晰的解答过程。使用Visual Studio 2019进行编译,成功实现了预期功能。

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

题目描述:

代码详解(对题目的解释详见相应代码的注释) :

#include<iostream>
using namespace std;
#include<vector>
#include<string>

//函数1,判断string对象是否含有大写字母,无需改变对象的值,所以使用常量引用
bool has_Upper(const string& s)
{
	bool ret = false;
	for (auto c : s)
	{
		if (isupper(c))
		{
			ret = true;
			break;
		}
	}
	return	ret;
}
//函数2,把string对象全部改成小写形式,需要改变对象的值,使用不含const的引用。因为要改变对象的值,所以必须使用引用
void to_Lower(string& s)
{
	for (auto c = s.begin(); c != s.end(); ++c)
	{
		*c = tolower(*c);
	}
}
void test03()
{
	//1.创建string对象
	string s = "cgbweio;Fhbw9p;";
	//2.验证函数1(有一个大写字母F,输出结果为1)
	cout << has_Upper(s) << endl;
	//3.验证函数2(有一个大写字母F,调用函数后变为小写字母f)
	to_Lower(s);
	cout << s << endl;
}
int main()
{
	test03();
	system("pause");
	return 0;
}

编译工具:Visual Studio 2019

运行结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值