Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java check palindrome with string builder

public static boolean isPalindrome(String str){  
    StringBuilder sb=new StringBuilder(str);  
    sb.reverse();  
    String rev=sb.toString();  
    if(str.equals(rev)){  
        return true;  
    }else{  
        return false;  
    }  
}  
Comment

String Palindrome in Java

import java.util.*;
import java.lang.*;
import java.io.*;


 class Main {

    static boolean isPalindrome(String s)
    {

       /* Two pointers initialised to 0 and 's.length()-1'.*/
        int i = 0, j = s.length() - 1;

        /* Looping through the string */
        while (i < j) {

            /* Even if one character is not equal, we will return false. */
            if (s.charAt(i) != s.charAt(j))
                return false;
               
            /* Value of pointers is changed */
            i++;
            j--;
        }
       
        /* This means all characters are same and thus string is a palindrome*/
        return true;
    }

    public static void main(String[] args)
    {
        String s = "abba";

        /* First, We'll convert it to lower case. */
        s = s.toLowerCase();
   
        if (isPalindrome(s))
            System.out.print("Yes");
        else
            System.out.print("No");
    }
}
Comment

generate palindrome java

//This function generates a palindrom by recursively reversing a String,
//recursively add it and checks if it's a palindrom

public static String generatePalindrom(String s) {
	return s.equals(reverseString(s)) ? s
				: generatePalindrom(String.valueOf(Integer.parseInt(s) + Integer.parseInt(reverseString(s))));
}

public static String reverseString(String s) {
	return s.length() == 1 ? s : s.charAt(s.length() - 1) + reverseString(s.substring(0, s.length() - 1));
}
Comment

PREVIOUS NEXT
Code Example
Java :: java mockito any string import 
Java :: javafx textarea size 
Java :: hello world! java 
Java :: change color of drawable android studio 
Java :: matrix multiplication javascrpt 
Java :: binary to decimal in java program 
Java :: java split string array 
Java :: java files 
Java :: diamond star pattern in java 
Java :: java throw keyword 
Java :: how to change checkbox color in android 
Java :: how to delete character in string java 
Java :: jbutton border size java 
Java :: java convert float to double 
Java :: find java_home path in cmd 
Java :: java.lang.ArrayIndexOutOfBoundsException 
Java :: method resolve file in java 
Java :: java append a string to file 
Java :: get index of element java 
Java :: java turn string into int 
Java :: apache commons collections android dependency 
Java :: java set textview style 
Java :: arraylist to linkedlist java 
Java :: how to check number format exception in java 
Java :: java stream and filter 
Java :: how to stop activity from another activity 
Java :: java convert am pm to 24 hour 
Java :: BottomNavigationView only icon 
Java :: java map create with values 
Java :: retrofit 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =