Drop links or images here to add them to the editor.

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.

Palindrome

Make

Write a program that allows the user to enter a word or sentence. The program outputs whether the input is a palindrome or not.

Success Criteria

Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.

Complete the subprogram called palindrome that:

  1. Takes a string parameter phrase to be processed.
  2. Returns true if phrase is a palindrome.
  3. Returns false if phrase is not a palindrome.

Note the algorithm will need to ignore case, whitespace and punctuation.

Complete the main program so that:

  1. The user can input a word, phrase or sequence of digits.
  2. The program outputs whether the phrase is a palindrome or not.

Typical inputs and outputs from the program would be:

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.
🆘 If you're really stuck, use this Parsons code sorting exercise
Complete program
// 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.");
---
        }
---
    }
---
}