【发布时间】:2017-06-03 16:03:08
【问题描述】:
我有一个问题,字符串的所有小写字符都应转换为大写字符。但根据问题,不应更改代码中的某些行。我已经写了下面的代码。
#include <iostream>
#include <string>
#include <stdio.h>
#include <ctype.h>
using namespace std;
class StringOps {
public:
void stringToUpper(string &s) //This line should not be changed
{
char c;
int i=0;
while (s[i])
{
c=s[i];
putchar (toupper(c));
i++;
}
}
};
int main(void)
{
string str ="Hello World";
StringOps obj;
obj.stringToUpper(str);
cout << str; //This line should not be changed
return 0;
}
我得到的输出为:
HELLO WORLDHello World
但是需要的输出是:
HELLO WORLD HELLO WORLD
如何制作
cout<<str;
main() 中的语句打印函数中计算的结果:
void stringToUpper(string &s)
【问题讨论】:
-
嗯,看起来您想修改
stringToUpper中的s,而不是仅仅从中读取。 -
您应该设法使
stringToUpper更改s而不是像现在这样打印。 -
谢谢!!我现在明白我的错误了。
标签: c++ string class output uppercase