Check if a string is a palindrome

后端 未结 30 1492
星月不相逢
星月不相逢 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: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;
        }
    

提交回复
热议问题