反转字符串

题目: Implement a function to reverts string like this : "This is a cat" => "sihT si a tac"
思路:1.题目要求单词的顺序不变,但是单词的字母要反转,首先考虑到栈的特性

   2.先实现单词的字母反转:把单词逐字分割,压入栈中,全部进栈后在pop出来

   3.保存单词,只需要将句子按空格分隔然后保存在数组中即可

 1         public static void RevertString()
 2         {
 3             string s = "This is a cat";
 4             string[] word = s.Split(' ');
 5             for (int i = 0; i < word.Count(); i++)
 6             {
 7                 Stack<string> w = new Stack<string> ();
 8                 foreach (var a in word[i])
 9                 {                   
10                     w.Push(a.ToString());
11                 }
12                 string top = w.Pop();
13                 while (top != string.Empty)
14                 {
15                     Console.Write(top);
16                     if (w.Count > 0)
17                     {
18                         top = w.Pop();
19                     }
20                     else break;
21                 }
22                 Console.Write(" ");
23             }
24             Console.WriteLine();
25         }
原文地址:https://www.cnblogs.com/hehe625/p/7810851.html