注册 登录
编程论坛 C++教室

[求助]求一个程序~~谢谢

kikyojlb 发布于 2007-06-10 21:19, 440 次点击

例如:
Welcome to China!

反过来输出:
China to Welcome!

要求:必须处理字符串在初始的储存string里

下面是例子
For example:

{

string a;

getline(cin,a); //if you input the string”Welcome to China!”

//your process

//………

cout<<a; //then the output is “China to Welcome!”

}

[此贴子已经被作者于2007-6-10 21:21:20编辑过]

2 回复
#2
游乐园2007-06-11 17:33

For example:

{


string a;
getline(cin, a); //if you input the string”Welcome to China!”

//your process
string::size_type pos = a.size()-1, pre_pos = pos;
int size =a.size();

while (pos=a.rfind(' ', pos))
{
a.append(a,pos, pre_pos);
pre_pos=--pos;
}
pos = a.find(' ', 0);
a.append(a, 0, pos-1);
a.erase(a.begin(), a.begin()+size+1);

cout<<a;//then the output is “China to Welcome!”

}

#3
kikyojlb2007-06-12 10:27

谢谢~~~~

1