Hi. I am learning C# programming language. I want to write palindrome program. I tried different code but none of them worked out. Please help me to get rid of this problem. Thanks in Advance.
Hi. I am learning C# programming language. I want to write palindrome program. I tried different code but none of them worked out. Please help me to get rid of this problem. Thanks in Advance.
Try out this code, when you run this program you get the word is palindrome or not:
Code:Palindrome using C#.net using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string str=string .Empty ; Console.WriteLine("Enter a String"); string s = Console.ReadLine(); int i = s.Length; //we can get the Length of string by using Length Property for (int j=i -1; j >= 0; j--) { str = str + s[j ]; } if (str == s) { Console.WriteLine(s + " is palindrome"); } else { Console.WriteLine(s + " is not a palindeome"); } Console.WriteLine(str); Console.Read(); } } }
Refer following program to check given string is palindrome or not:
Code:bool CheckPalindrome (string CheckString) { if (CheckString == null || CheckString.Length == 0) { return false; } for (int i = 0; i < CheckString.Length / 2; i++) { if (CheckString[i] != CheckString[CheckString.Length - 1 - i]) { return false; } } return true; }
private static void CheckPalindrome()
{
string s1 = string.Empty;
Console.WriteLine("Please enter a string: ");
s1 = Console.ReadLine();
int length = s1.Length;
bool flag=false;
int j = length - 1;
for (int i = 0; i < (length/2); i++)
{
if (s1[i] != s1[j--])
{
flag=true;
break;
}
Console.WriteLine("{0}",j);
}
if (flag)
Console.WriteLine("String is not a Palindrome");
else
Console.WriteLine("String is Palindrome");
}
Bookmarks