Check if a string is a palindrome

后端 未结 30 1350
星月不相逢
星月不相逢 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:18
    protected bool CheckIfPalindrome(string text)
    {
        if (text != null)
        {
            string strToUpper = Text.ToUpper();
            char[] toReverse = strToUpper.ToCharArray();
            Array.Reverse(toReverse );
            String strReverse = new String(toReverse);
            if (strToUpper == toReverse)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
    

    Use this the sipmlest way.

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

    Out of all the solutions, below can also be tried:

    public static bool IsPalindrome(string s)
    {
        return s == new string(s.Reverse().ToArray());
    }
    
    0 讨论(0)
  • 2020-11-28 09:18
    static void Main(string[] args)
    {
        string str, rev="";
    
        Console.Write("Enter string");
    
        str = Console.ReadLine();
    
        for (int i = str.Length - 1; i >= 0; i--)
        {
            rev = rev + str[i];
        }
    
        if (rev == str)
            Console.Write("Entered string is pallindrome");
        else
            Console.Write("Entered string is not pallindrome");
    
        Console.ReadKey();
    }
    
    0 讨论(0)
  • 2020-11-28 09:20
    string test = "Malayalam";
                char[] palindrome = test.ToCharArray();
                char[] reversestring = new char[palindrome.Count()];
                for (int i = palindrome.Count() - 1; i >= 0; i--)
                {
                    reversestring[palindrome.Count() - 1 - i] = palindrome[i];
    
                }
    
                string materializedString = new string(reversestring);
    
                if (materializedString.ToLower() == test.ToLower())
                {
                    Console.Write("Palindrome!");
                }
                else
                {
                    Console.Write("Not a Palindrome!");
                }
    
                Console.Read();
    
    0 讨论(0)
  • 2020-11-28 09:21

    Just for fun:

    return myString.SequenceEqual(myString.Reverse());
    
    0 讨论(0)
  • 2020-11-28 09:21
    public bool Solution(string content)
        {
            int length = content.Length;
    
            int half = length/2;
    
            int isOddLength = length%2;
    
            // Counter for checking the string from the middle 
            int j = (isOddLength==0) ? half:half+1;
    
            for(int i=half-1;i>=0;i--)
            {                
                if(content[i] != content[j])
                {
                   return false;
                }
                j++;
    
            }
            return true;
        }
    
    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题