问题 E: C#判断回文字符串

题目描述

使用C#编写一个静态方法。该方法能够判断字符串是否是“回文”(即顺读和逆读相同的字符串)。

输入

一个字符串;

输出

如果是回文字符串,则输出“yes”,否则输出“no”;

样例输入

abcdcab

样例输出

no

提示

(1)用string类的toCahrArray()方法,将字符串转换为字符数组。(2)使用StringBuilder类保存逆序后的字符串。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace 判断回文字符串
{
    class Program
    {
        static void Main(string[] args)
        {
            int flag = 0;
            string s = Console.ReadLine();
            char[] c = s.ToCharArray();
            for (int i = 0, j = c.Length - 1; i < j; ++i, --j)
            {
                if (c[i] != c[j])
                {
                    flag = 1;
                    break;
                }
            }
            if (flag == 1) Console.WriteLine("no");
            else    Console.WriteLine("yes");
 
            Console.ReadKey();
        }
    }
}

  

原文地址:https://www.cnblogs.com/mjn1/p/12523922.html