使用栈判断回文字符串
class Program { static void Main(string[] args) { //判断回文数 CStack myStack=new CStack(); string word = "sys"; string ch; bool isHuiWen =true; //栈是LIFO,字符串入栈,出栈时最后一个字符先出。 for (int i = 0; i < word.Length; i++) { myStack.Push(word[i]); } int pos = 0; //出栈字符依次和原字符串字符比较 while (pos < myStack.Count) { ch = myStack.Pop().ToString(); if (ch != word[pos].ToString()) { isHuiWen = false; break; } pos++; } if (isHuiWen) Console.WriteLine("Dream has no boundary."); else Console.WriteLine("Let technology and science records our life."); Console.ReadLine(); } } |