Check if a string is a palindrome

后端 未结 30 1347
星月不相逢
星月不相逢 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:28
    public static bool palindrome(string t)
        {
            int i = t.Length;
            for (int j = 0; j < i / 2; j++)
            {
                if (t[j] == t[i - j-1])
                {
                    continue;
                }
                else
                {
                    return false;
                    break;
                }
            }
            return true;
        }
    
    0 讨论(0)
  • 2020-11-28 09:31
    public Boolean IsPalindrome(string value)
    {
       var one = value.ToList<char>();
       var two = one.Reverse<char>().ToList();
       return one.Equals(two);
    }
    
    0 讨论(0)
  • 2020-11-28 09:34

    //This c# method will check for even and odd lengh palindrome string

    public static bool IsPalenDrome(string palendromeString)
            {
                bool isPalenDrome = false;
    
                try
                {
                    int halfLength = palendromeString.Length / 2;
    
                    string leftHalfString = palendromeString.Substring(0,halfLength);
    
                    char[] reversedArray = palendromeString.ToCharArray();
                    Array.Reverse(reversedArray);
                    string reversedString = new string(reversedArray);
    
                    string rightHalfStringReversed = reversedString.Substring(0, halfLength);
    
                    isPalenDrome = leftHalfString == rightHalfStringReversed ? true : false;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
    
                return isPalenDrome;
            }
    
    0 讨论(0)
  • 2020-11-28 09:34
        public  bool MojTestPalindrome (string word)
        {
            bool yes = false;
    
            char[]test1 = word.ToArray();
            char[] test2 = test1.Reverse().ToArray();
            for (int i=0; i< test2.Length; i++)
            {
    
                if (test1[i] != test2[test2.Length - 1 - i])
                {
    
                    yes = false;
                    break;
    
                }
                else {   
                    yes = true;
    
    
                }
            }
            if (yes == true)
            {
                return true;
            }
            else
    
                return false;                
        }
    
    0 讨论(0)
  • 2020-11-28 09:36

    If you just need to detect a palindrome, you can do it with a regex, as explained here. Probably not the most efficient approach, though...

    0 讨论(0)
  • 2020-11-28 09:37

    String extension method, easy to use:

        public static bool IsPalindrome(this string str)
        {
            str = new Regex("[^a-zA-Z]").Replace(str, "").ToLower();
            return !str.Where((t, i) => t != str[str.Length - i - 1]).Any();
        }
    
    0 讨论(0)
提交回复
热议问题