Check if a string is a palindrome

后端 未结 30 1346
星月不相逢
星月不相逢 2020-11-28 08:59

I have a string as input and have to break the string in two substrings. If the left substring equals the right substring then do some logic.

How can I do this?

相关标签:
30条回答
  • 2020-11-28 09:14

    Here is an absolutely simple way to do this,

    1. Receive the word as input into a method.
    2. Assign a temp variable to the original value.
    3. Loop through the initial word, and add the last character to the reversal that you are constructing until the inital word has no more characters.
    4. Now use the spare you created to hold the original value to compare to the constructed copy.

    This is a nice way as u don't have to cast ints and doubles. U can just pass them to the method in their string representation by using the ToString() method.

    public static bool IsPalindrome(string word)
        {
            string spare = word;
            string reversal = null;
            while (word.Length > 0)
            {
                reversal = string.Concat(reversal, word.LastOrDefault());
                word = word.Remove(word.Length - 1);
            }
            return spare.Equals(reversal);
        }
    

    So from your main method, For even and odd length strings u just pass the whole string into the method.

    0 讨论(0)
  • 2020-11-28 09:15
    public static  bool IsPalindrome(string word)
            {
                //first reverse the string
                string reversedString = new string(word.Reverse().ToArray());
                return string.Compare(word, reversedString) == 0 ? true : false;
            }
    
    0 讨论(0)
  • 2020-11-28 09:16
    public static bool getStatus(string myString)
    {
        string first = myString.Substring(0, myString.Length / 2);
        char[] arr   = myString.ToCharArray();
    
        Array.Reverse(arr);
    
        string temp   = new string(arr);
        string second = temp.Substring(0, temp.Length / 2);
    
        return first.Equals(second);
    }
    
    0 讨论(0)
  • 2020-11-28 09:17

    In C# :

    public bool EhPalindromo(string text)
    {
     var reverseText = string.Join("", text.ToLower().Reverse());
     return reverseText == text;
    }
    
    0 讨论(0)
  • 2020-11-28 09:18

    use this way from dotnetperls

      using System;
    
        class Program
        {
            /// <summary>
            /// Determines whether the string is a palindrome.
            /// </summary>
            public static bool IsPalindrome(string value)
            {
                int min = 0;
                int max = value.Length - 1;
                while (true)
                {
                    if (min > max)
                    {
                        return true;
                    }
                    char a = value[min];
                    char b = value[max];
    
                    // Scan forward for a while invalid.
                    while (!char.IsLetterOrDigit(a))
                    {
                        min++;
                        if (min > max)
                        {
                            return true;
                        }
                        a = value[min];
                    }
    
                    // Scan backward for b while invalid.
                    while (!char.IsLetterOrDigit(b))
                    {
                        max--;
                        if (min > max)
                        {
                            return true;
                        }
                        b = value[max];
                    }
    
                    if (char.ToLower(a) != char.ToLower(b))
                    {
                        return false;
                    }
                    min++;
                    max--;
                }
            }
    
            static void Main()
            {
                string[] array =
                {
                    "A man, a plan, a canal: Panama.",
                    "A Toyota. Race fast, safe car. A Toyota.",
                    "Cigar? Toss it in a can. It is so tragic.",
                    "Dammit, I'm mad!",
                    "Delia saw I was ailed.",
                    "Desserts, I stressed!",
                    "Draw, O coward!",
                    "Lepers repel.",
                    "Live not on evil.",
                    "Lonely Tylenol.",
                    "Murder for a jar of red rum.",
                    "Never odd or even.",
                    "No lemon, no melon.",
                    "Senile felines.",
                    "So many dynamos!",
                    "Step on no pets.",
                    "Was it a car or a cat I saw?",
    
                    "Dot Net Perls is not a palindrome.",
                    "Why are you reading this?",
                    "This article is not useful.",
                    "...",
                    "...Test"
                };
    
                foreach (string value in array)
                {
                    Console.WriteLine("{0} = {1}", value, IsPalindrome(value));
                }
            }
        }
    
    0 讨论(0)
  • 2020-11-28 09:18

    That is non-trivial, there is no built in method to do that for you, you'll have to write your own. You will need to consider what rules you would like to check, like you implicitly stated you accepted reversing of one string. Also, you missed out the middle character, is this only if odd length?

    So you will have something like:

    if(myString.length % 2 = 0)
    {
         //even
         string a = myString.substring(0, myString.length / 2);
         string b = myString.substring(myString.length / 2 + 1, myString.lenght/2);
    
         if(a == b)
               return true;
    
         //Rule 1: reverse
         if(a == b.reverse()) //can't remember if this is a method, if not you'll have to write that too
               return true;
    

    etc, also doing whatever you want for odd strings

    0 讨论(0)
提交回复
热议问题