8 Replies - 707 Views - Last Post: 21 July 2009 - 03:39 PM Rate Topic: -----

#1 Krissilla  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 24-January 09

palindrome

Posted 21 July 2009 - 12:10 AM

What String i'll use in inputting and describing a palindromE????? ;) ;) ;)

This post has been edited by Krissilla: 21 July 2009 - 12:23 AM


Is This A Good Question/Topic? 0
  • +

Replies To: palindrome

#2 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: palindrome

Posted 21 July 2009 - 12:13 AM

.. Proper caps and an understandable question may help.
Was This Post Helpful? 0
  • +
  • -

#3 Krissilla  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 24-January 09

Re: palindrome

Posted 21 July 2009 - 12:23 AM

What String i'll use in inputting and describing a palindromE?????
Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: palindrome

Posted 21 July 2009 - 02:02 AM

do you mean, how do know if the inputted string is a palindrome or not?
Was This Post Helpful? 0
  • +
  • -

#5 Krissilla  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 24-January 09

Re: palindrome

Posted 21 July 2009 - 04:32 AM

View Postmostyfriedman, on 21 Jul, 2009 - 01:02 AM, said:

do you mean, how do know if the inputted string is a palindrome or not?



yeS may i?? :D :D :D
Was This Post Helpful? 0
  • +
  • -

#6 kmangold  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 167
  • Joined: 24-June 09

Re: palindrome

Posted 21 July 2009 - 04:37 AM

You can do it iteratively yourself by comparing character for character. Or you can use StringBuffer's reverse() method and compare them backwards and forwards.
Was This Post Helpful? 0
  • +
  • -

#7 Krissilla  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 24-January 09

Re: palindrome

Posted 21 July 2009 - 04:48 AM

import java.io.*;
public class Mp2 {

  public static boolean isPalindrome(String stringToTest) {
	String workingCopy = removeJunk(stringToTest);
	String reversedCopy = reverse(workingCopy);

	return reversedCopy.equalsIgnoreCase(workingCopy);
  }

  protected static String removeJunk(String string) {
	int i, len = string.length();
	StringBuffer dest = new StringBuffer(len);
	char c;

	for (i = (len - 1); i >= 0; i--) {
	  c = string.charAt(i);
	  if (Character.isLetterOrDigit(c)) {
		dest.append(c);
	  }
	}

	return dest.toString();
  }

  protected static String reverse(String string) {
	StringBuffer sb = new StringBuffer(string);

	return sb.reverse().toString();
  }

  public static void main(String[] args) {
   System.out.println("Input a phrase/word:");

	String string =" ";

	
	System.out.println("	" + string);
	System.out.println();

	if (isPalindrome(string)) {
	  System.out.println("It IS a palindrome!");
	} else {
	  System.out.println("It is NOT a palindrome!");
	}
	System.out.println();
  }
}


 


public static void main(String[] args) {
   System.out.println("Input a phrase/word:");

	String string =" ";

	
	System.out.println("	" + string);
	System.out.println();

	if (isPalindrome(string)) {
	  System.out.println("It IS a palindrome!");
	} else {
	  System.out.println("It is NOT a palindrome!");
	}
	System.out.println();
  }
}


i made a research on the codes.. and im stuck in that code box above..
i wanted that the user will have to input a phrase/word and after that is to identify whether it is a palindrome or not, .

This post has been edited by Krissilla: 21 July 2009 - 04:50 AM

Was This Post Helpful? 0
  • +
  • -

#8 AbuJaFaR  Icon User is offline

  • D.I.C Regular

Reputation: 13
  • View blog
  • Posts: 330
  • Joined: 13-December 07

Re: palindrome

Posted 21 July 2009 - 05:35 AM

Search for Scanner examples so you can read the input from the user.It is something like that: (after you import the java.util.Scanner)

String line;
Scanner input=new Scanner(System.in);
line=input.nextLine();



Because now in your main, you are not reading anything.It is proper to include the Scanner operation inside a try-catch block. :)
Was This Post Helpful? 0
  • +
  • -

#9 chili5  Icon User is offline

  • D.I.C Lover

Reputation: 19
  • View blog
  • Posts: 1,144
  • Joined: 28-December 07

Re: palindrome

Posted 21 July 2009 - 03:39 PM

You can use a Stack to do this or a loop starting at the end of the string comparing char from the beginning with a char at the end. If they are not the same, they are not palindromes.

I like to use the string builder's reverse method and compare the return value with the original value.

String s = "test";
if (new StringBuilder(s).reverse().toString().equals(s)) {
 // is a palindrome
} else {
  // not
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1