Algorithm to determine if string given by user is palindromic

Question:
Develop an algorithm using English pseudo code Java to determine if a string of characters given by the user is palindromic.

Algorithm outputs suitable message.

Blank characters and punctuation should be ignored when processing the string.

Answer:
This looks suspiciously like a homework assignment, but I’ll give it ashot anyway.

You’ll have to decide how the string to be tested is going to be enteredby the user. One idea is to have a simple GUI with a text field into which userscan type a string. Your reply could appear in a second text field.Here’s a sketch of the code:

public class PalTester extends Frame {   TextField palField, answerField;   PalTester() {      palField = new TextField(“”, 20);      answerField = new TextField(“”, 20);      add(palField);      add(answerField);   }   public boolean action(Event e, Object arg) {      if (e.target().equals(palField))         if (palindrome(palField.getText()))            answerField.setText(“palindrome”);         else            answerField.setText(“not palindrome”);   }   private boolean palindrome(String s) { … }   public static void main() {      PalTester f = new PalTester();      f.resize(300, 300);      f.show();   }}
All the work goes on inside the palindrome tester.
private boolean palindrome(String s) { … }
Actually, what you really want is something that detects weak palindromes.A weak palindrome is a palindrome after all non-alphanumerics have beenremoved and all letters have been converted to lowercase. For example,after this comression, the string:
Doc note. I dissent. A fast never prevents a fatness. I diet on cod!
becomes the palindrome:
docnoteidissentafastneverpreventsafatnessidietoncod
Once this compression has been done, it’s easy to detect a palindrome;merely compare the string to its reverse. Here’s the pseudo code:
private boolean palindrome(String s) {      String s1 = compress(s);      String s2 = s1.toLowerCase();      String s3 = reverse(s2);      return s2.compareTo(s3);   }
You’ll need two supporting functions:
private String compress(String s) { …}
and
private String reverse(String s) { … }
In either case, if s is the empty string, there’s no work to be done.Otherwise, recursively reverse or compress s.subString(1), then eitherconcatenate s.substring, 0, 0) onto the end of this result for reverseor, in the case of compress, concatenate it onto the beginning of thisresult if it’s an alphanumeric.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS