1. 程式人生 > >C# 判斷是否為迴文

C# 判斷是否為迴文

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();
        }
    }
}

執行結果: