Palindrome In C#

Palindrome

Any word, number, or phrase is a palindrome if it reads the same backward or forward; this means you  can read it either left to right or right to left.

In the below code, we are reversing the entered string in text box and then comparing both the strings. If both strings are equal, then the string is a palindrome. The implemented logic is for case sensitive strings.

The program is successfully compiled and executed with Microsoft Visual Studio 2015. The output of the program is also shown below with screenshot.

Example

malayalam, civic, noon, radar, level, kayak, 15851, ec252ce, etc. -- all these strings are palindrome.

There are many logical ways to check the palindrome. But, I have described only two methods here. Both the logics are for case-sensitive phrases.

Method 1

HTML Code

  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2.   
  3. <head runat="server">  
  4.     <title></title>  
  5. </head>  
  6.   
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.         <div>  
  10.             <asp:Label ID="lblMessage" runat="server" Text="" Font-Bold="true" ForeColor="Green"></asp:Label><br />  
  11.             <asp:Label ID="Label1" runat="server" Text="Enter String"></asp:Label>  
  12.             <asp:TextBox ID="txtString" runat="server"></asp:TextBox><br /><br />  
  13.             <asp:Button ID="btnSubmit" runat="server" Text="Check Palindrome" OnClick="btnSubmit_Click" />  
  14.             <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please Enter string" ControlToValidate="txtString" ForeColor="#cc3300"></asp:RequiredFieldValidator>  
  15.         </div>  
  16.     </form>  
  17. </body>  
  18.   
  19. </html>  
C# code
  1. protected void btnSubmit_Click(object sender, EventArgs e)  
  2. {  
  3.     CheckPalindrome(txtString.Text.Trim());  
  4. }  
  5. protected void CheckPalindrome(string strOriginal) {  
  6.     string strReverse = "";  
  7.     int len = strOriginal.Length;  
  8.     // First Reverse the original string  
  9.     for (int i = len - 1; i >= 0; i--) {  
  10.         strReverse = strReverse + strOriginal[i].ToString();  
  11.     }  
  12.     // if reversed string and original string are same then string is Palindrome.  
  13.     if (strOriginal == strReverse) {  
  14.         lblMessage.Text = "Entered string is Palindrome";  
  15.     } else {  
  16.         lblMessage.Text = "Entered string is not Palindrome";  
  17.     }  
  18. }  
Download the source code, open it with Visual Studio, and run the application. Enter the string in the textbox and click on “Check Palindrome” button. The result will be in green text. See Fig1 below.


Method 2

Check Palindrome using inbuilt function in single line code.
  1. public static bool IsPalindrome(string str)  
  2. {  
  3.    return str.SequenceEqual(str.Reverse());  
  4. }  
The above function is made by using C#.NET inbuilt function SequenceEqual() and Reverse(). The function SequenceEqual() has been used to compare strings and function Reverse() reverses the string. If the string is a Palindrome, then the function will return ‘true’ otherwise it will return false.

Thanks !!! I hope it will help.