字符串翻轉
來源:程序員人生 發布時間:2014-10-06 08:00:01 閱讀次數:2418次
原文地址: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 ;
}
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈