Algorithm to determine if string given by user is palindromic

Algorithm to determine if string given by user is palindromic

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.

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 (         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);;   }}
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:
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) { …}
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.

See also  Does It Make Sense to Splurge on a Laptop?

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist