C++Primer Plus 编程练习8.8,练习3 引用传参的方法

3.编写一个函数,它接受一个指向 string对象的引用作为参数,并将该 string 对象的内容转换为大写
为此可使用表6.4描述的函数toupper()。然后编写一个程序,它通过使用一个循环让您能够用不同的输入来测试这个函数,
该程序的运行情况如下:
Enter a string(q to quit) : go away
GO AWAY
Next string(q to quit) : good grief!
GOOD GRIEF!
Next string(q to quit) : q
Bye.

#pragma region 练习3.cpp
/*
*/
#if 1
#include <iostream>
#include<string>
using namespace std;
string& CoutUper(string& str)
{
	for (int i = 0; i < strlen(str.c_str()); i++)
	{
		str[i] = toupper(str[i]);
	}
	return str;
}

int main()
{
	while (true)
	{
		cout << "输入一个字符串(q to quit):";
		string strInput;
		getline(cin, strInput);
		if (!cin||"q" == strInput||"Q"==strInput)
		{
			break;
		}
		cout << CoutUper(strInput) << endl;
	}
	cout << endl;
	return 0;
}
#endif
#pragma endregion

求字符串长度的时候,用strlen()之前需要将string字符串转换为char类型的用到c_str()的方法

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值