Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Algorithms
Expertise: Beginner
May 28, 1997

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 a shot anyway.

You'll have to decide how the string to be tested is going to be entered by the user. One idea is to have a simple GUI with a text field into which users can 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 been removed 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 either concatenate s.substring, 0, 0) onto the end of this result for reverse or, in the case of compress, concatenate it onto the beginning of this result if it's an alphanumeric.
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date