检查一个字符串是否是回文字符串

29
我有一个字符串作为输入,需要将该字符串分成两个子字符串。如果左侧子字符串等于右侧子字符串,则执行一些逻辑。
我该如何实现?
示例:
public bool getStatus(string myString)
{

}

例子: myString = "ankYkna",如果我们把它分成两个子字符串,它们将是: left-part = "ank", right-part = "ank"(反转后)。


你在这里遇到的实际问题是什么?只需使用 myString.SubString() - ken2k
@ken2k 如何将一个字符串分割成两个相等的子字符串。 - ankur
1
所以你只是想检查这个字符串是否是一个字谜吗? - Thomas Levesque
实际上我指的是回文,不是变位词...感谢cadrell0给出正确的单词 ;) - Thomas Levesque
我得到了那个东西,这就是重点。 - ankur
目前的答案都没有正确识别出所有回文,只是回文的一个子集。我已经包含了所有回文的解决方案,例如回文句子。 - Patrick Kelly
33个回答

0
    public static bool IsPalindrome(string str)
    {
        int i = 0;
        int a = 0;

        char[] chr = str.ToCharArray();
        foreach (char cr in chr)
        {
            Array.Reverse(chr);
            if (chr[i] == cr)
            {
                if (a == str.Length)
                {
                    return true;
                }
                a++;
                i++;
            }
            else
            {
                return false;
            }
        }
        return true;
    }

0
使用传统方法查找回文的最短技巧
public string IsPalindrome(string Word)
{
    int i = 0, j = Word.Length - 1;
    for (; i < Word.Length - 1 && j >= 0 && Word[i] == Word[j]; i++, j--) ;
    return j == 0 ? "Palindrome" : "Not Palindrome";
}

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


class palindrome
{
    static void Main(string[] args)
    {
        Console.Write("Enter a number:");
        string panstring = Console.ReadLine();
        Palindrome(panstring);
        Console.ReadKey();
    }
     static int index = 0;
     public static void Palindrome(string strexcluding)
    {
        try
        {
            string reversecounter = string.Empty;

            for (int i = strexcluding.Length - 1; i >= 0; i--)
            {
                if (strexcluding[i].ToString() != null)
                    reversecounter += strexcluding[i].ToString();
            }
            if (reversecounter == strexcluding)
            {
                Console.WriteLine("Palindrome Number: " + strexcluding);
            }
            else
            {
                Sum(strexcluding);
            }
        }
         catch(Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }

    public static void Sum(string stringnumber)
    {
        try
        {
            index++;
            string number1 = stringnumber;
            string number2 = stringnumber;
            string[] array = new string[number1.Length];
            string obtained = string.Empty;
            string sreverse = null;

            Console.WriteLine(index + ".step : " + number1 + "+" + number2);

            for (int i = 0; i < number1.Length; i++)
            {
                int temp1 = Convert.ToInt32(number1[number1.Length - i - 1].ToString());
                int temp2 = Convert.ToInt32(number2[number2.Length - i - 1].ToString());

                if (temp1 + temp2 >= 10)
                {
                    if (number1.Length - 1 == number1.Length - 1 - i)
                    {
                        array[i] = ((temp1 + temp2) - 10).ToString();
                        obtained = "one";
                    }
                    else if (number1.Length - 1 == i)
                    {
                        if (obtained == "one")
                        {
                            array[i] = (temp1 + temp2 + 1).ToString();
                        }
                        else
                        {
                            array[i] = (temp1 + temp2).ToString();
                        }
                    }
                    else
                    {
                        if (obtained == "one")
                        {
                            array[i] = ((temp1 + temp2 + 1) - 10).ToString();
                        }
                        else
                        {
                            array[i] = ((temp1 + temp2) - 10).ToString();
                            obtained = "one";
                        }

                    }
                }

                else
                {
                    if (obtained == "one")
                        array[i] = (temp1 + temp2 + 1).ToString();
                    else
                        array[i] = (temp1 + temp2).ToString();
                    obtained = "Zero";
                }

            }

            for (int i = array.Length - 1; i >= 0; i--)
            {
                if (array[i] != null)
                    sreverse += array[i].ToString();
            }

            Palindrome(sreverse);
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接