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()的方法