首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

資料結構 (C#):堆疊應用範例——字串反轉

     編輯:關於C語言

應用示例:字串反轉

char[] ch = "hello world!".ToCharArray();
Stack<char> s = new Stack<char>();

foreach (char c in ch) { s.Push(c); }

int idx = 0;
while (!s.IsEmpty) { ch[idx++] = s.Pop(); }

Console.WriteLine(new string(ch)); // !dlrow olleh

利用堆疊後進先出特性,將字元逐一壓入再彈出,達成字串反轉。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved