C# 判斷是否為迴文
阿新 • • 發佈:2019-02-09
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string a; a=Console.ReadLine(); int len = a.Length-1; int x,y; for (x = 0,y = len - x; x <= (len+1)/ 2; ++x,--y) { if (a[x]!=a[y]) { Console.WriteLine("不是迴文"); break; } } if(x>=y) { Console.WriteLine("是迴文"); } Console.ReadKey(); } } }
執行結果: