In this blog, we will learn How to write a program to check whether a String is a Palindrome or not in C#. A palindrome is a word or phrase or a sequence of characters that reads the same either read from backward or as in forwards e.g. Malayalam, Madam, etc.
Console.WriteLine("Enter
a string to check either it's Palindrome or not"); string statement = Console.ReadLine(); //variable to hold the reversed statement string reverseStatement = ""; //Reverse the statement for (int i=statement.Length-1;i>=0;i--) { reverseStatement +=
statement[i]; } //Check statement and reverse statement are equal if
(statement.Equals(reverseStatement, StringComparison.OrdinalIgnoreCase)) { Console.WriteLine("'{0}' is an example of Palindrome", statement); } else { Console.WriteLine("'{0}' is not an example of Palindrome", statement); } //Console.ReadLine() to hold the screen after the execution of
the program Console.ReadLine(); |
Console.WriteLine("Enter
a string to check either it's Palindrome or not"); string statement = Console.ReadLine(); //variable to hold the reversed statement string reverseStatement = ""; //Convert the string to the Char Array Char[] charArr = statement.ToCharArray(); //Reverse the Array Array.Reverse(charArr); //Convert the character array to string reverseStatement = new string(charArr); //Check statement and reverse statement are equal if
(statement.Equals(reverseStatement, StringComparison.OrdinalIgnoreCase)) { Console.WriteLine("'{0}' is an example of Palindrome", statement); } else { Console.WriteLine("'{0}' is not an example of Palindrome", statement); } //Console.ReadLine() to hold the screen after the execution of
the program Console.ReadLine(); |
0 comments:
Post a Comment