Check if a string is a palindrome

后端 未结 30 1480
星月不相逢
星月不相逢 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

     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"); 
    
            }
    

提交回复
热议问题