The Artima Developer Community
Sponsored Link

Java Answers Forum
help on a palindrome program, please?

1 reply on 1 page. Most recent reply: May 16, 2005 4:41 PM by Stan James

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Josh Friedman

Posts: 1
Nickname: kratos2005
Registered: May, 2005

help on a palindrome program, please? Posted: May 4, 2005 6:56 PM
Reply to this message Reply
Advertisement
i'm writing a program that will recognize a palindrome when input by JOptionPane. it will recognize the phrase as palindrome if it is one and will tell me if the phrase is not a palindrome correctly. the problem is that i need to get the phrase to be recognized with spaces and punctuation which i haven't been able to do up to this point. my teacher gave me the ascii chart for upper case and lower case charts of conversion, but that doesn't help me with spaces and punctuation. i have looked up the charts for the other codes, but i don't know what to do how to implement them into the program. here is the code:


import javax.swing.JOptionPane;
import java.io.*;
 
class Prog7c{
	public static void main(String[] args){
		//the declaration and JOptionPane and limit
		String str = new String();
		str = JOptionPane.showInputDialog("Please Enter In A Phrase.");
		int limit = str.length()-1;
		//printing statement
		System.out.println("Forwards:  " + str);
		//the palindrome
		boolean flag = true;
		String strLower = str.toLowerCase();
		String strNew = "";
//area in question below: the two for loops and the two char declarations
		char c = 32;
		char d = 47;
		for(int k=0;k<=limit;k++){
			if((strLower.charAt(k) >= 'a') && (strLower.charAt(k)<='z') && (strLower.charAt(k)>= c) && (strLower.charAt(k)<= d)){
				strNew = strNew + strLower.charAt(k);
			}
		}
		for(int j=0;j<=limit/2;j++){
			if(strLower.charAt(j) != strLower.charAt(limit - j)){
				flag = false;
			}
		}
		if(flag){
			System.out.println("\n\nThe Phrase is a Palindrome.");
		}
		else
			System.out.println("\n\nThe Phrase is NOT a Palindrome.");
			
		System.exit(0);
	}
}


Stan James

Posts: 4
Nickname: j3
Registered: Feb, 2004

Re: help on a palindrome program, please? Posted: May 16, 2005 4:41 PM
Reply to this message Reply
I like your idea of copying the string with spaces and punctuation removed. Take a look at the Character class, isLetter() method. See if that helps you decide which characters to keep and which to discard.

Then be sure you're checking your strNew when you test for the palindrome.

Flat View: This topic has 1 reply on 1 page
Topic: Runtime.exec() method Previous Topic   Next Topic Topic: creating a virtual key

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use