首页 > 代码库 > 字符串倒序

字符串倒序

将 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

 

字符串倒序