Check if a string is a palindrome

后端 未结 30 1349
星月不相逢
星月不相逢 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:22
    int length = myString.Length;
    for (int i = 0; i < length / 2; i++)
    {
        if (myString[i] != myString[length - i - 1])
            return false;
    }
    return true;
    
    0 讨论(0)
  • 2020-11-28 09:22
     private void CheckIfPalindrome(string str) 
            {
                //place string in array of chars
                char[] array = str.ToCharArray(); 
                int length = array.Length -1 ;
                Boolean palindrome =true;
                for (int i = 0; i <= length; i++)//go through the array
                {
                    if (array[i] != array[length])//compare if the char in the same positions are the same eg "tattarrattat" will compare array[0]=t with array[11] =t if are not the same stop the for loop
                    {
                        MessageBox.Show("not");
                        palindrome = false;
                        break;
    
                    }
                    else //if they are the same make length smaller by one and do the same 
                    {                   
                      length--;
                    }
    
                }
                if (palindrome) MessageBox.Show("Palindrome"); 
    
            }
    
    0 讨论(0)
  • 2020-11-28 09:23

    Using LINQ and off course far from the best solution

    var original = "ankYkna";
    var reversed = new string(original.Reverse().ToArray());
    var palindrom = original == reversed;
    
    0 讨论(0)
  • 2020-11-28 09:24

    This way is both concise in appearance & processes very quickly.

    Func<string, bool> IsPalindrome = s => s.Reverse().Equals(s);
    
    0 讨论(0)
  • 2020-11-28 09:27
    class Program
    {
        static void Main(string[] args)
        {
    
            string s, revs = "";
            Console.WriteLine(" Enter string");
            s = Console.ReadLine();
            for (int i = s.Length - 1; i >= 0; i--) //String Reverse
            {
                Console.WriteLine(i);
                revs += s[i].ToString();
            }
            if (revs == s) // Checking whether string is palindrome or not
            {
                Console.WriteLine("String is Palindrome");
            }
            else
            {
                Console.WriteLine("String is not Palindrome");
            }
            Console.ReadKey();
        }
    }
    
    0 讨论(0)
  • 2020-11-28 09:27
        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 讨论(0)
提交回复
热议问题