THINK FIRST·CODE LATER

← All labs

StringBuilder: reverse and palindrome

Problem

Write a class Palindrome with a method

public static boolean isPalindrome(String text)

that returns true when the text reads the same in both directions, ignoring case, spaces and punctuation (keep only letters and digits). Use StringBuilder and its reverse() method.

main reads one line and prints:

Reversed: amanaplanacanalpanama
Palindrome: true

Reversed shows the cleaned text reversed (lower case, letters and digits only).

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l13-2-data-1.txt

A man, a plan, a canal: Panama
What your program must print for l13-2-data-1.txt
Palindrome: true

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l13-2-data-1.txt

Windows PowerShell does not understand <:

Get-Content l13-2-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.