Results 1 to 5 of 5
  1. #1
    trinity is offline Member
    Join Date
    Apr 2011
    Rep Power

    Default Palindrome program

    Hi there , I have just written a palindrome program to check if a specific String is or not a palindrome . My program seems fare to complicated for such a simple task. Is there a more conventional way of doing this?
    Java Code:
    import tcdIO.*;
    public class Palyndrome {
    	public static void main(String args[]) {
    		String string;
    		Terminal t = new Terminal();
    		string = t.readString("Enter the word you want to test\n>");
    		int counter = string.length() - 1;
    		int remainder;
    		int palyndrome = 0;
    		remainder = string.length() % 2;
    		for (int i = 0; i < string.length() / 2 + remainder; i++) {
    			while (counter >= string.length() / 2 + remainder) {
    				if (string.charAt(i) != string.charAt(counter)) {
    			t.println(""+string+" is a palyndrome");
    				t.println(""+string+" is  not a palyndrome");}

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Washington, DC
    Rep Power


    I would just have two indexes- start (initialized to zero) and end (initialized to the length of the string minus one). In a loop, check that the char at index start and the char at index end are equal. If they aren't, the String is not a palindrome. Increment start and decrement end before looping again. When start is greater than or equal to end, then the String is a palindrome.

    But that's just how I do it by hand or in my head, without any code. If you do it a different way (think about how you decide whether some text is a palindrome or not without any code, just in your head), then that's how your program should work.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Rep Power


    Would this be cheating?
    Java Code:
    boolean palindrome = text.equals(new StringBuilder(text).reverse().toString());

  4. #4
    Maximus-EVG is offline Member
    Join Date
    Apr 2011
    Rep Power


    You could push the string which you're testing onto a stack data structure, char by char, and after its done, pop the stack and form another string with it.
    Then compare the two strings.

    Read up on Stack datastructure here:
    Stack (Java 2 Platform SE 5.0)

    (not as nice as dlorde's solution, but at least with this youre doing "work" :D. Actually Im pretty sure the .reverse() method does the same thing as I wrote).

  5. #5
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Atlanta, Georgia, US
    Rep Power


    Hehe you misspelled Palindrome ;)

    Anyway, here's my 4-lines of code to check palindrome:
    Java Code:
    public boolean isPalindrome(String s) {
        for(int a = 0; a < s.length()/2; a++)
            if(s.charAt(a) != s.charAt(s.length()-a-1))
                return false;
        return true;

Similar Threads

  1. Palindrome program help
    By Nel in forum New To Java
    Replies: 1
    Last Post: 03-03-2011, 03:49 AM
  2. Palindrome question
    By luke in forum New To Java
    Replies: 20
    Last Post: 10-02-2010, 01:17 AM
  3. Palindrome
    By pinkdreammsss in forum Java Applets
    Replies: 8
    Last Post: 05-04-2010, 03:59 PM
  4. StringTokenizer in a Palindrome program
    By jeremyk in forum New To Java
    Replies: 10
    Last Post: 02-13-2010, 06:35 PM
  5. HELP...Palindrome
    By d7o0om in forum New To Java
    Replies: 12
    Last Post: 11-13-2009, 03:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts