5

Im trying to create a palindrome checker. I am using a StringBuilder and I've discovered that appending spaces are kind of tricky.

EDIT: are there other ways besides using .reverse()? Thanks for the answers. :D

This code works when the word has no spaces:

public String palindrome (String anyString) {

StringBuilder sb = new StringBuilder();

for (int i = anyString.length()-1; i>=0; i--) {
        sb.append(anyString.charAt(i));
}

String string2 = sb.toString();

return string2;
}

When the word entered has a space; it returns a string that includes characters up to the first space.

E.g.:

word = "not a palindrome"
palindrome(word) = "emordnilap"

expected = "emordnilap a ton"


I have tried inserting

if (anyString.charAt(i) != ' ')
    sb.append(anyString.charAt(i));
else 
    sb.append(' ');

at the middle of the code but it doesn't work.

Thanks guys!

Kai
  • 38,985
  • 14
  • 88
  • 103
user1993177
  • 573
  • 2
  • 6
  • 9
  • Are you sure your code does not work??Its working fine for me. :D – Anubhab Mar 06 '13 at 14:46
  • Please describe what behavior your expect of a 'palindrome checker'. Should it verify if a given string is a palindrome? Or if it contains whitespace-separated palindromes? Or should it just generate the palindrome of the string like your are attempting above? In the latter case, why is it called a 'checker'? – Adriaan Koster Mar 06 '13 at 16:27

8 Answers8

8

Use StringBuilder.reverse() method instead, it works faster (1378 line) and more correct

StringBuilder sb = new StringBuilder("not a palindrome");
System.out.println(sb.reverse());

Output:

emordnilap a ton

bsiamionau
  • 8,099
  • 4
  • 46
  • 73
3

The reverse method is already built into StringBuilder

public String palindrome (String anyString) {
    StringBuilder sb = new StringBuilder(anyString);
    return sb.reverse().toString();
}
Cesar Loachamin
  • 2,740
  • 4
  • 25
  • 33
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
3
public static boolean isPalindromeNaive(String s){
    StringBuilder sb = new StringBuilder(s);
    StringBuilder sbReverse = sb.reverse();
    if (s.equals(String.valueOf(sbReverse))){
        return true;
    }
    return false;
}

Doesn't seem to be what you are looking for but it's the complete method just for reference.

Mona Jalal
  • 34,860
  • 64
  • 239
  • 408
2

This code works:

public boolean isPalindrome(String word){
    return word.equals(new StringBuilder(word).reverse().toString().trim()) ? true : false;
}
1

This answer allows you to just not count the characters you don't want.

public boolean isPalindrome(String s) {
  for (int left = 0, right = s.length()-1; ; left++, right--) {
    while(left < right && !isAcceptableCharacter(s.charAt(left))) {
      left++;
    }
    while(left < right && !isAcceptableCharacter(s.charAt(right))) {
      right++;
    }
    if (left >= right) { return true; }
    if (s.charAt(left) != s.charAt(right)) { return false; }
  }
}

static boolean isAcceptableCharacter(char c) {
  return Character.isLetter(c) || Character.isDigit(c);
}
Olivier Grégoire
  • 33,839
  • 23
  • 96
  • 137
0

You should pass the argument in either all in lowerCase or all in UpperCase or You can also write new method for converting String, StringBuffer, StringBuilder

//Method for remove spaces "\t" tabsapce and "\n" newline "\f"...

private StringBuilder removeUnwanted(StringBuilder strBuild){
            String str = new String(strBuild);
            str = str.replace("\n","").replace("\t","").replace("\f","").replace(" ","");
            return new StringBuilder(str);
}
public boolean isPal(StringBuilder strBuild){
            int flag = 0;
            strBuild = removeUnwanted(strBuild);
            for(int i = 0;i<strBuild.length()-1;i++){
                    if(strBuild.charAt(i) != strBuild.reverse().charAt(i)){
                            flag = 1;
                            break;
                    }
            }
            if(flag == 0)
                    return true;
            else
                    return false;
}
Koder
  • 1
  • 2
0

this is the simplest method to check.

import java.util.*;
public class Main {

public static void main(String[] args) {
// write your code here
    Scanner s = new Scanner(System.in);

    System.out.println("Enter the string");

    String st1 = s.nextLine();

    String st2 = Palindrome(st1);
    if (isPalindrome(st1,st2)==true)
    {
        System.out.println("palindrome");
    }
    else {
        System.out.println("Not palindrome");
    }
}

private static String Palindrome(String s) {
    StringBuilder stringBuilder = new StringBuilder(s);
    return String.valueOf(stringBuilder.reverse());
}

private static boolean isPalindrome(String s1, String s2){
    if (s1.equals(s2))
    {
        return true;
    }
    else {
        return false;
    }
}
}
Bikram Nath
  • 483
  • 6
  • 15
0

If you just want a palindrome checker without String Builder

private static boolean isPalindrome(String input) {

        for (int i = 0, j = input.length() - 1; j >= i; i++, j--) {
            if (input.charAt(i) != input.charAt(j))
                return false;
        }

        return true;
}

But StringBuilder should work:

private static boolean isPalindrome(StringBuilder input) {
    return input.toString().equals(input.reverse().toString());
}
csoler
  • 393
  • 2
  • 9