Sunday 21 June 2015

TESTDOME - PALINDROME

TEST C#:
Write a function that checks if a given sentence is a palindrome. A palindrome is a word, phrase, verse, or sentence that reads the same backward or forward. Only the order of English alphabet letters (A-Z and a-z) should be considered, other characters should be ignored.
For example, IsPalindrome("Noel sees Leon.") should return true as spaces, period, and case should be ignored resulting with "noelseesleon" which is a palindrome since it reads same backward and forward.

SOLUTION (100%):

public class Palindrome
{
    public static bool IsPalindrome(string str)
    {        
        str = str.ToLower();
        var newStr = String.Join("", str.Where(char.IsLetterOrDigit));
        int l = newStr.Length;
        var reverse = String.Empty;       
        
        for (int i= l-1; i>=0; i--) reverse +=newStr[i];
        
        if (newStr == reverse) return true;
        else return false;            
            
    }

    public static void Main(string[] args)
    {      
        Console.WriteLine(IsPalindrome("Noel sees Leon."));
    }
}

PR

1 comment:

  1. Palindrome.cs(9,42): error CS1061: 'string' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?

    ReplyDelete