码迷,mamicode.com
首页 > 其他好文 > 详细

字符串倒序

时间:2017-04-02 13:14:39      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:stack   main   top   display   space   src   ack   style   else   

将 I am Beijing. 倒序为Beijing. like I 

用栈实现:

技术分享
 1 #include <iostream>
 2 #include <stack>
 3 #include <string>
 4 using namespace std;
 5 int main()
 6 {
 7     string str = "I like Beijing.";
 8     str =   + str;
 9     string newStr = "";
10     stack<char> st;
11     for (int i = str.length() - 1; i >= 0; i--)
12     {
13         char c = str.at(i);
14         if (c !=   )
15         {
16             st.push(c);
17         }
18         else {
19             string temp = "";
20             while (!st.empty())
21             {
22                 temp += st.top();
23                 st.pop();
24             }
25             newStr += temp  +  ;
26         }
27     }
28     cout<<newStr<<endl;
29 }
View Code

 

字符串倒序

标签:stack   main   top   display   space   src   ack   style   else   

原文地址:http://www.cnblogs.com/AndrewGhost/p/6658759.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!