首页 > 试题广场 >

编写一个函数,它接受一个指向string对象的引用作为参数,

[问答题]
编写一个函数,它接受一个指向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.


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

void uppercase(string &st);

int main()
{
	string st;
	cout<<"Enter a string(q to quit)";
	getline(cin,st);
	while (st!="q")
	{
		uppercase(st);
		cout<<st<<endl;
		cout<<"Next string:(q to quit)";
		getline(cin,st);
	}
	cout<<"Bye"<<endl;
	system("pause");
	return 0;
}

void uppercase(string &st)
{
	for (int i = 0; i < st.size(); i++)
	{
		st[i]=toupper(st[i]);
	}
}

发表于 2021-03-06 16:19:44 回复(0)