题目描述:
![](https://img-blog.csdnimg.cn/direct/a98aaaa6dac243438dc758248fbceb18.png)
代码实现:
#include <iostream>
#include <stack>
#include <string>
using namespace std;
void CopyStack(stack<char>& s, stack<char>& s1)
{
while (!s.empty())
{
s1.push(s.top()); //将s栈顶依次拷贝进s1栈底
s.pop(); //删除s栈顶元素
}
while (!s1.empty())
{
if(s1.top()!=' ')
cout << s1.top(); //输出s1栈顶元素
else
cout << "%20"; //替换空格
s1.pop(); //删除s1栈顶元素
}
}
int main()
{
string str; //用str储蓄输入
stack<char> s, s1; //创建栈s和s1
int i;
getline(cin, str); //输入字符串
for (i = 0; i < str.length(); i++) {
s.push(str[i]); //将字符压入栈
}
CopyStack(s, s1);
return 0;
}
运行结果:
![](https://img-blog.csdnimg.cn/direct/708d208fe216486995c4891221a6efc1.png)