The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

palindrome

Posted by nate on December 03, 2001 at 8:21 PM

> I am having problems creating a stack and queue. The problem is as the following:

> A Palindrome is a word which reads the same both backwords and fowards. One way to check whether a word is a palindrome is to take each letter of the word one at a time and to place them onto both a stack and a queue. The letters are then read back from these structures and, if all letters match, the word is a palindrome.e.g tot.

>
> Three seperate classes are to be designed and developed.
> The first class is to be called CharStack which will have the following public methods:

> CharStack() - the constructor for the class which creates an empty stack.
> void pushChar(char ch) - pushes the single chracter ch onto the stack.
> char popChar() - pops the top character of the stack and returns it.
> void destroyStack() - removes any remaining contents of the stack.
> boolean isEmptyStack() - returns True if the stack is empty, otherwise False.

> The second class is to be called CharQueue and will offer the following public methods:

> CharQueue() - the constructor for the class which creates an empty queue.
> void addChar(char ch) - adds the single chracter ch to the queue.
> char popChar() - takes the head character of the queue and returns it.
> void destroyQueue() - removes any remaining contents of the queue.
> boolean isEmptyQueue() - returns True if the queue is empty, otherwise False.

> The final class is to be a Java application which uses the above two classes. It should allow the user to type in a word (of an unknown length) and will use the above method of checking whether that word is a palindrome and will report the results to the user. The programme should exit when # is types in.

> I will be greatful if you can solve my problem by writing this programme.

> thank you for your time.

// Palindrome.java
// determine if input strings are palindromes
public class Palindrome
{
public static void main(String[] args)
{
do {
System.out.print("Enter a sentence: ");
String sentence = SavitchIn.readLine();
if (isPalindrome(sentence))
System.out.println("That is a palindrome.");
else
System.out.println("That is not a palindrome.");
} while (keepGoing());
} // main
// return true if s is a palindrome
private static boolean isPalindrome(String s)
{
String simple = ""; // will hold just the letters in s
String reverse = ""; // will hold the letters of s in reverse
char c;
s = s.toLowerCase();
for (int i = 0; i < s.length(); ++i)
{
c = s.charAt(i);
// could use: if (Character.isLetter(c))
if ((c >= 'a') && (c <= 'z'))
{
simple = simple + c;
reverse = c + reverse;
}
}
return (simple.equals(reverse));
} // isPalindrome
// return true if the user wants to continue
private static boolean keepGoing()
{
System.out.print("Enter another sentence? [y/n] ");
char answer = SavitchIn.readLineNonwhiteChar();
return ((answer == 'y') || (answer == 'Y'));
} // keepGoing
} // class





Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us