Answers for "how to check palindrome string in java"

7

Java program to check palindrome string using recursion

import java.util.Scanner;
public class RecursivePalindromeJava 
{
   // to check if string is palindrome using recursion
   public static boolean checkPalindrome(String str)
   {
      if(str.length() == 0 || str.length() == 1)
         return true; 
      if(str.charAt(0) == str.charAt(str.length() - 1))
         return checkPalindrome(str.substring(1, str.length() - 1));
      return false;
   }
   public static void main(String[]args)
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter a string : ");
      String strInput = sc.nextLine();
      if(checkPalindrome(strInput))
      {
         System.out.println(strInput + " is palindrome");
      }
      else
      {
         System.out.println(strInput + " not a palindrome");
      }
      sc.close();
   }
}
Posted by: Guest on November-02-2020
6

Java program to check whether string is palindrome using library methods

public class StringPalindromeJava
{
   public static void isPalindrome(String str)
   {
      String strReverse = new StringBuffer(str).reverse().toString();
       // checking for palindrome
      if(str.equals(strReverse))
      {
         System.out.println(str + " is palindrome string.");
      }
      else
      {
         System.out.println(str + " is not palindrome string.");
      }
   }
   public static void main(String[] args)
   {
      // palindrome java
      isPalindrome("eye");
      isPalindrome("rotator");
   }
}
Posted by: Guest on November-02-2020
1

palindrome number in java

import java.util.Scanner;

public class Palindrome
{
  public static void main(String args[])
  {
    int num,temp,reverse=0;
    Scanner input=new Scanner(System.in);
    num=in.nextInt();
    temp=num;
    //code to reverse the number
    while(temp!=0)
    {
      int d=temp%10; //extracts digit at the end
      reverse=reverse*10+d;
      temp/=10; //removes the digit at the end
    }
    // 'reverse' has the reverse version of the actual input, so we check
    if(reverse==num)
    {
      System.out.println("Number is palindrome");
    }
    else
    {
      System.out.println("Number is not palindrome");
    }
  }
}
Posted by: Guest on August-20-2020
2

palindrome function java

package test
//The function below checks if a string is a palindrome
//True = Is a palindrome & False = Not a palindrome
	public boolean isPalindromString(String text){
    	String reverse = reverse(text);
      	if(text.equals(reverse))
    	{
        	return true;
      	}
      
      	return false;
    }
//This function returns the reverse String of its input.
//Ex. if given "hello", it will return "olleh"
	public String reverse(String input)
    {     
		if(input == null || input.isEmpty())
        {
			return input;
        }
        	return input.charAt(input.length()- 1) + reverse(input.substring(0, input.length() - 1));
    }
Posted by: Guest on June-18-2020
0

integer palindrome in java

public boolean isPalindrome(int x) {
        String str = Integer.toString(x);
        String reverse =  new StringBuilder(str).reverse().toString();
        return  str.equals(reverse)?true:false;}
Posted by: Guest on April-26-2021
0

palindrome in java

public class PlaindromeTest1 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
System.out.println("Enter your words"); // Whats up body? // ?ydob pu stahW
        String word = input.nextLine();

        String reversedWord = "";
        // start from last index number.
        for(int i = word.length()-1; i>=0; i-- ){
           reversedWord += word.charAt(i);
        }
        System.out.println(reversedWord);
        //Level ==> leveL //ey edip adanada pide ye :)
        boolean palindrome = word.equalsIgnoreCase(reversedWord);

        System.out.println(palindrome);

        String str = "Anna";
        String result = "";

        for(int i = str.length()-1; i>=0; i-- ){
            result += str.substring(i, i+1);
            // result += str.charAt(i); // char at de olur
        }
        boolean result1 = str.equalsIgnoreCase(result);
        System.out.println(result1);
    }
Posted by: Guest on June-04-2021

Code answers related to "how to check palindrome string in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language