Palindrome Checker using StringBuilder
Asked Answered
T

8

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!

Tensive answered 6/3, 2013 at 14:20 Comment(2)
Are you sure your code does not work??Its working fine for me. :DLukin
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'?Brotherinlaw
S
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

Stickinthemud answered 6/3, 2013 at 14:22 Comment(0)
C
3

The reverse method is already built into StringBuilder

public String palindrome (String anyString) {
    StringBuilder sb = new StringBuilder(anyString);
    return sb.reverse().toString();
}
Calends answered 6/3, 2013 at 14:23 Comment(0)
L
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.

Leaseholder answered 26/5, 2016 at 1:24 Comment(0)
A
2

This code works:

public boolean isPalindrome(String word){
    return word.equals(new StringBuilder(word).reverse().toString().trim()) ? true : false;
}
Aspa answered 15/1, 2022 at 1:53 Comment(0)
A
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);
}
Astrict answered 14/7, 2017 at 17:11 Comment(0)
H
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;
}
Henkel answered 14/7, 2017 at 16:36 Comment(0)
P
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;
    }
}
}
Purapurblind answered 19/3, 2018 at 16:13 Comment(0)
W
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());
}
Wilone answered 4/7, 2021 at 3:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.