字符串翻转
发布时间:2020-12-13 20:20:58 所属栏目:PHP教程 来源:网络整理
导读:原文地址:http://blog.csdn.net/wangyuling1234567890/article/details/39610373 将字符串翻转,如下: 输入:Hi Welcome to cricode 输出:cricode to Welcome Hi #include iostream#include string#include stackusing std::cout;using std::endl;using s
原文地址:http://blog.csdn.net/wangyuling1234567890/article/details/39610373 将字符串翻转,如下: 输入:Hi Welcome to cricode 输出:cricode to Welcome Hi #include <iostream>
#include <string>
#include <stack>
using std::cout;
using std::endl;
using std::string;
using std::stack;
void main()
{
string str("Hi Welcome to cricode");
stack<char> cstack;
stack<char> tmp;
int index = 0;
for (index = 0; index < str.size(); index++)
{
cstack.push(str[index]);
}
index = 0;
while(!cstack.empty())
{
if (' ' == cstack.top()) // 事例代码,未对标点符号做判断
{
while(!tmp.empty())
{
str[index++] = tmp.top();
tmp.pop();
}
str[index++] = ' ';
cstack.pop();
}
else
{
tmp.push(cstack.top());
cstack.pop();
}
}
while(!tmp.empty())
{
str[index++] = tmp.top();
tmp.pop();
}
cout<<str<<endl;
return ;
} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |