Answers for "palindrom in java"

6

easy palindrome program in java

import java.util.*;   
class PalindromeExample2  
{  
   public static void main(String args[])  
   {  
      String original, reverse = ""; // Objects of String class  
      Scanner in = new Scanner(System.in);   
      System.out.println("Enter a string/number to check if it is a palindrome");  
      original = in.nextLine();   
      int length = original.length();   
      for ( int i = length - 1; i >= 0; i-- )  
         reverse = reverse + original.charAt(i);  
      if (original.equals(reverse))  
         System.out.println("Entered string/number is a palindrome.");  
      else  
         System.out.println("Entered string/number isn't a palindrome.");   
   }  
}
Posted by: Guest on August-28-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

is palindrome java

public static boolean checkPalindrome(String str)
{     
 
    int len = str.length();

    for(int i = 0; i < len / 2; i++)  {
        if (str.charAt(i) != str.charAt(len - i - 1))
            return false;
    }
    return true;
}
Posted by: Guest on September-28-2021
1

String palindrome in java using array

// String palindrome in java using array
import java.util.Arrays;
import java.util.Scanner;
public class StringPalindromeUsingArray 
{
   public static void main(String[] args) 
   {
      System.out.println("Please enter string to check whether string is palindrome: ");
      Scanner sc = new Scanner(System.in);
      String strInput = sc.nextLine();
      char[] chArray = strInput.toCharArray();
      int size = chArray.length;
      char[] chGiven = Arrays.copyOf(chArray, chArray.length);
      for(int a = 0; a < size / 2; a++) 
      {
         char temp = chArray[a];
         chArray[a] = chArray[size - a - 1];
         chArray[size - a - 1] = temp;
      }
      System.out.println("Given array: " + Arrays.toString(chGiven));
      System.out.println("String palindrome using array: " + Arrays.toString(chArray));
      if(Arrays.equals(chArray, chGiven)) 
      {
         System.out.println("string is palindrome.");
      }
      else 
      {
         System.out.println("string is not a palindrome.");
      }
      sc.close();
   }
}
Posted by: Guest on February-22-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language