Search This Blog

Monday, August 9, 2021

Palindrome

 Q: Write a code to check whether a string is palindrome or not?
====================================================================================

public void PalindrometrickOne()
{
    //string word = "MADAMPO";
    string word = "MADAM";
    int wordlength = word.Length;

    // forloop and check i and length - i character are same or not

    for (int i = 0; i < wordlength / 2; i++)
    {
        if (word[i] != word[wordlength - 1 - i])
        {
            // if for once its not, exit the loop and give error .
            Console.WriteLine("Word is not palindrom");
            return; //it will be exit form the whole method. 
        }
    }

    // reach only if 'return' never get executed. 
    Console.WriteLine("The given word is palindrome");
}

No comments:

Post a Comment