Palindrome validation with StringBuilder

I am trying to create a palindrome check. I am using StringBuilder and I found that the added spaces are quite complex.

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

This code works when a 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 containing characters up to the first space.

eg:.

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

expected = "emordnilap a ton"


I tried to insert

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

in the middle of the code, but it does not work.

Thanks guys!

+5
source share
6 answers

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

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

Conclusion:

emordnilap a ton

+7

reverse StringBuilder

public String palindrome (String anyString) {
    StringBuilder sb = new StringBuilder(anyString);
    return sb.reverse().toString();
}
+2
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;
}

, , .

+1

, .

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);
}
+1
source

You must pass the argument to either lowerCase or UpperCase, or you can also write a new method to convert String, StringBuffer, StringBuilder

// Method for removing 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;
}
0
source

This is the easiest way 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;
    }
}
}
0
source

All Articles