It reads the same forwards and backwards.
★★★A palindrome is a word, number or phrase that reads the same backwards as forwards, such the words madam or racecar.
Write a program that allows the user to enter a word or sentence. The program outputs whether the input is a palindrome or not.
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
palindrome that:phrase to be processed.phrase is a palindrome.phrase is not a palindrome.Note the algorithm will need to ignore case, whitespace and punctuation.
Enter a phrase:
ABBA
ABBA is a palindrome.
Enter a phrase:
02/02/2020
02/02/2020 is a palindrome.
Enter a phrase:
A man, a plan, a canal – Panama
A man, a plan, a canal – Panama is a palindrome.
Enter a phrase:
Hello World
Hello World is not a palindrome.
// Palindrome program
using System;
class Submission
{
---
// -------------------------
// Subprograms
// -------------------------
---
static bool palindrome(string phrase)
{
---
// Convert to lowercase for like-for-like comparisons
phrase = phrase.ToLower();
bool is_palindrome = true;
---
// Set indexes to beginning and end of the phrase
int letter_front = 0;
int letter_back = phrase.Length - 1;
---
// Check all the letters up to half way or if the phrase is not a palindrome
while (letter_front < phrase.Length / 2 && is_palindrome)
{
---
// Skip over any characters that are not alphanumerics
while (!char.IsLetterOrDigit(phrase[letter_front]))
{
---
letter_front++;
---
}
---
while (!char.IsLetterOrDigit(phrase[letter_back]))
{
---
letter_back--;
---
}
---
// If the two letters are not the same the phrase is not a palindrome
if (phrase[letter_front] != phrase[letter_back])
{
---
is_palindrome = false;
---
}
---
// Increment and decrement the indexes
letter_front++;
letter_back--;
---
}
---
return is_palindrome;
---
}
---
// -------------------------
// Main program
// -------------------------
public static void Main(string[] args)
{
---
Console.WriteLine("Enter a phrase: ");
string phrase = Console.ReadLine();
---
if (palindrome(phrase))
{
---
Console.WriteLine($"{phrase} is a palindrome.");
---
}
---
else
{
---
Console.WriteLine($"{phrase} is not a palindrome.");
---
}
---
}
---
}