String Palindrome in C#
String Palindrome in C#
Write a program to check if a given string is a palindrome in c# .
using System;
public class Program
{
public static void Main()
{
string Orginal = "ISI";
char[] arr = Orginal.ToCharArray();
Array.Reverse(arr);
bool isPalindrome = (Orginal == new string(arr));
string results = isPalindrome ? "Palindrome" : "Not Palindrome";
Console.WriteLine(Orginal + " is " + results);
}
}
Using LINQ:
Write a program to check if a given string is a palindrome using LINQ.
string.Join combines the reversed characters into a string.
StringComparison.OrdinalIgnoreCase makes the comparison case-insensitive.
This reverses the characters of the string str and creates a new string (reversed).
string reversed = string.Join("", str.Reverse());
This compares the original string str with the reversed string reversed, ignoring case differences, to check if they are the same (i.e., if the string is a palindrome).
if (str.Equals(reversed, StringComparison.OrdinalIgnoreCase))
using System;
using System.Linq;
public class Program
{
public static void Main()
{
Console.Write("Enter a string: ");
string str = Console.ReadLine();
string reversed = string.Join("", str.Reverse());
if (str.Equals(reversed, StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("The string is a palindrome.");
}
else
{
Console.WriteLine("The string is not a palindrome.");
}
}
}
The string is a palindrome.
Comments
Post a Comment