Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Java program to check whether the given string is a palindrome or not

public static void main(String[] args) throws ParseException {
    String word;
    Scanner in = new Scanner(System.in);
    System.out.println("Enter a string to check if it is a palindrome");
    word = in.nextLine();
    System.out.println(palindromeChecker(word));
}
public static String palindromeChecker(String word){
    int length = word.length();
    String reverse="";
    for ( int i = length - 1 ; i >= 0 ; i-- )
        reverse = reverse + word.charAt(i);
    if (word.equals(reverse))
        return "Palindrome";
    else
        return "Not a Palindrome";
}
Comment

check palindrome in java

class Solution {
    public boolean isPalindrome(int x) {
        if(x < 0)
            return false;

        return x == reverseNum(x);

    }
    private static int reverseNum(int num){
        int rev = 0;
        while(num>0){
            int digit = num%10;
            rev = (rev * 10) + digit;
            num = num/10;
        }
        return rev;
    }
}
Comment

Longest Palindrome String in java

class Solution {
   String getLongestPalindrome(String s) {
       int n = s.length();
       int index  = 0, palindromeLength = 1;
       for (int i = 1; i < n; i++) {
           int left = i - 1, right = i;
           while(left >= 0 && right < n && s.charAt(left) == s.charAt(right)) {
               if(right - left + 1 > palindromeLength) {
                   index = left;
                   palindromeLength = right - left + 1;
               }
               left--;
               right++;
           }
           left = i - 1;
           right = i + 1;
           while(left >= 0 && right < n && s.charAt(left) == s.charAt(right)) {
               if(right - left + 1 > palindromeLength) {
                   index = left;
                   palindromeLength = right - left + 1;
               }
               left--;
               right++;
           }
           
       }
       String ans = "";
       for (int i = index; i < index + palindromeLength; i++) {
           ans += s.charAt(i);
       }
       return ans;
   }
}
Comment

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;}
Comment

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;
}
Comment

palindrome find in java

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int num = input.nextInt();
        int temp = num;

        int rev = 0;
        while (num > 0) {
            rev = rev * 10 + num % 10;
            num /= 10;
        }
        System.out.println(temp==rev?"palindrome":"not palindrome");
    }
Comment

Palindrome java

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Please input a word: ");
    String line = keyboard.nextLine();

    String cleanLine = line.replaceAll("[W]", "");
    String reverse = new StringBuilder(cleanLine).reverse().toString();
    boolean isPalindrome = cleanLine.equals(reverse);

    System.out.print("It is " + isPalindrome + " that this word is a palindrome.");
}
Comment

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;
     }
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

Palindrome Program in Java.

/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
    public static boolean isPalindrome(String str)
    {
        // Initializing an empty string to store the reverse
        // of the original str
        String rev = "";
 
        // Initializing a new boolean variable for the
        // answer
        boolean ans = false;
 
        for (int i = str.length() - 1; i >= 0; i--) {
            rev = rev + str.charAt(i);
        }
 
        // Checking if both the strings are equal
        if (str.equals(rev)) {
            ans = true;
        }
        return ans;
    }
    public static void main(String[] args)
    {
        // Input string
        String str = "geeks";
 
        // Convert the string to lowercase
        str = str.toLowerCase();
        boolean A = isPalindrome(str);
        System.out.println(A);
    }
}
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

Longest Palindrome String in java

class Solution {
   String getLongestPalindrome(String s) {
       int n = s.length();
       int index  = 0, palindromeLength = 1;
       for (int i = 1; i < n; i++) {
           int left = i - 1, right = i;
           while(left >= 0 && right < n && s.charAt(left) == s.charAt(right)) {
               if(right - left + 1 > palindromeLength) {
                   index = left;
                   palindromeLength = right - left + 1;
               }
               left--;
               right++;
           }
           left = i - 1;
           right = i + 1;
           while(left >= 0 && right < n && s.charAt(left) == s.charAt(right)) {
               if(right - left + 1 > palindromeLength) {
                   index = left;
                   palindromeLength = right - left + 1;
               }
               left--;
               right++;
           }
           
       }
       String ans = "";
       for (int i = index; i < index + palindromeLength; i++) {
           ans += s.charAt(i);
       }
       return ans;
   }
}
Comment

palindrome in java

package com.candidjava; import java.util.Scanner; public class PalindromeUptoN{	public static void main(String[] args)	{		int n, b, rev = 0;		Scanner sc = new Scanner(System.in);		System.out.println("Enter the Palindrome No N:");		int N = sc.nextInt();		System.out.print("Palindrome numbers from 1 to N:");		for (int i = 1; i <= N; i++)		{			n = i;			while (n > 0)			{				b = n % 10;				rev = rev * 10 + b;				n = n / 10;			}			if (rev == i)			{				System.out.print(i + " ");			}			rev = 0;		}	} }
Comment

PREVIOUS NEXT
Code Example
Java :: spring boot get request body 
Java :: imageview.setbackground 
Java :: his version of the Android Support plugin for IntelliJ IDEA (or Android Studio) cannot open this project, please retry with version 4.2 or newer. 
Java :: get list data in java 
Java :: in java how to compare two strings 
Java :: java rename file 
Java :: how to convert a string to char in java 
Java :: Conversion from Java List to Array 
Java :: java php object 
Java :: GenerationTarget encountered exception accepting command : Error executing DDL 
Java :: convert java to python 
Java :: float java 
Java :: check if field exists in java 
Java :: java.lang.IllegalArgumentException: View=DecorView@5fd145b[MainActivity] not attached to window manager 
Java :: fibonacci series in java 
Java :: haxe interface 
Java :: équivalent setTimeInterval java 
Java :: add to list java 
Java :: window in java swing 
Java :: Syntax of how to create ArrayList in Java 
Java :: java lambda expressions 
Java :: exception in thread "main" java.lang.unsupportedclassversionerror: has been compiled by a more recent version of the java runtime (class file version 55.0), this version of the java runtime only recognizes class file versions up to 52.0 
Java :: reading string after double in java 
Java :: write file java 
Java :: kruskal java 
Java :: java try-with-resources 
Java :: how to get orientation lock to portrait android stackoverflow 
Java :: how to convert a string of characters to a stream of binary characters binary 
Java :: bukkit random 
Java :: interface java 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =