问题描述:编写一个函数,他接受一个指向string对象的引用作为参数,并将该string对象的内容转换为大写字母,编写一个程序可以循环测试这个函数,直到用户输入q则程序结束
解决方法:1.建立一个函数,通过检测用户输入,使用函数toupper()将小写字母转换为大写字母输出
2.主函数中创建一个读到q才会跳出的循环,循环中不断向用户询问输入一句话,输入到string数组中,接着调用函数,转换为大写输出
代码:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
string a;
void f(string &a)
{
int i = 0;
for (int i = 0; a[i];i++)
{
a[i]=toupper(a[i]);
}
cout << a<<endl;
}
int main()
{
while (1)
{
cout << "请输入一句话"<<endl;
getline(cin, a);
if (a == "q")
break;
f(a);
}
cout << "程序结束"<<endl;
return 0;
}