Exercise
Function Palindrome, Iterative
Objective
Write an C# iterative function to say whether a string is symmetric (a palindrome). For example, "RADAR" is a palindrome.
Write Your C# Exercise
C# Exercise Example
// Import the System namespace to use basic classes like Console
using System;
class Program
{
// Function to check if a string is a palindrome (symmetric)
public static bool Palindrome(string str)
{
// Convert the string to uppercase to make the comparison case-insensitive
str = str.ToUpper();
// Loop through half of the string and compare each character from both ends
for (int i = 0; i < str.Length / 2; i++)
{
// Compare the character at position i with the character at the corresponding position from the end
if (str[i] != str[str.Length - i - 1])
{
// If any character does not match, it's not a palindrome
return false;
}
}
// If all characters match, it's a palindrome
return true;
}
// Main function to test the Palindrome function
public static void Main()
{
// Test case 1: "RADAR"
string testString1 = "RADAR";
Console.WriteLine($"Is \"{testString1}\" a palindrome? {Palindrome(testString1)}");
// Test case 2: "HELLO"
string testString2 = "HELLO";
Console.WriteLine($"Is \"{testString2}\" a palindrome? {Palindrome(testString2)}");
// Test case 3: "madam"
string testString3 = "madam";
Console.WriteLine($"Is \"{testString3}\" a palindrome? {Palindrome(testString3)}");
}
}