OJ Problem 3452 C# to determine the palindrome string

Title description

Use C# to write a static method. This method can determine whether the string is a "palindrome" (that is, the same string is read in sequence and in reverse).

enter

A string

Output

If it is a palindrome string, output "yes", otherwise output "no";

Sample input

abcdcab

Sample output

no

prompt

(1) Use the toCahrArray() method of the string class to convert the string to a character array. (2) Use the StringBuilder class to save the reversed string.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {      
        static void Main(string[] args)
        {
            //1.输入字符串转换为字符数组
            string str1 = Console.ReadLine();
            char[] strs1 = str1.ToCharArray();
            //2.使用StringBuilder类保存逆序后的字符串
            StringBuilder Str = new StringBuilder();
            for(int i=strs1.Length-1;i>=0;i--)
            {
                Str.Append(strs1[i]);
            }
            //3.将逆序后的字符串转换为字符数组
            string str2 = Str.ToString();
            char[] strs2 = str2.ToCharArray();
            //4.判断逆序前后的字符串是否相同
            for(int i=0;i<strs1.Length;i++)
            {
                if(strs1[i]!=strs2[i])
                {
                    Console.WriteLine("no");
                    return;
                }
            }
            Console.WriteLine("yes");
        }
    }
}

 

Guess you like

Origin blog.csdn.net/wangws_sb/article/details/104815051