Your Java Programmers Are Here


package com.journaldev.util;

public class Palindrome {

public static boolean isPalindrome(String str) {

if (str == null)

return false;

StringBuilder strBuilder = new StringBuilder(str);

strBuilder.reverse();

return strBuilder.toString().equals(str);

}

public static void main(String[] args) {

System.out.println(“Is aba palindrome? ” + Palindrome.isPalindrome(“aba”));

System.out.println(“Is abc palindrome? ” + Palindrome.isPalindrome(“abc”));}}

import java.util.Scanner;

public class Palindrome {

public static void main(String[] args) {

do {

Scanner sc = new Scanner(System.in);

System.out.println(“Enter the string: “);

String str = sc.nextLine();

StringBuffer sb = new StringBuffer(str);

sb.reverse();

if (str.equalsIgnoreCase(sb.toString())) {

System.out.println(“Entered string is a palindrome”);

} else {

System.out.println(“Entered string is not a palindrome”);

}

} while (askAgain());

}

private static boolean askAgain() {

Scanner sc = new Scanner(System.in);

System.out.println(“Do you want to check another palindrome? Yes/No”);

String answer = sc.nextLine();

if (answer != null && answer.length() > 0) {

if (answer == “Yes” || answer == “yes”) {

return true; // answer

package palindrome;

import java.util.Scanner;

public class Palindrome {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print(“Enter a word: “);

String word = input.nextLine();

int low = 0; //starting point of the string

int high = word.length() – 1; //ending point of the string

boolean isPalindrome = true; // to check if it is a palindrome or not

while (low < high) { if (word.charAt(low) != word.charAt(high)) { //check each character from starting and ending and compare them together isPalindrome = false; //if the characters are not same then its not a palindrome break;// break out of the loop as soon as we know it is not a palindrome } low++; //incrementing starting point by one till it becomes equal to ending point import java.io.*; class Palindrome { public static void main(String args[])throws IOException { int rev=0,rem,n; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the number:"); n=Integer.parseInt(br.readLine()); int temp=n; while(n>0)

{

rem=n%10;

rev=rev*10+rem;

n/=10;// n=n/10 also correct.

}

if(temp==rev) // n is redundant as temp stores the initial value of n that we assigned in line 6 and rev is the reverse of this value. You can use any of them here and it will work fine. I used temp because it is the original name of the variable used in line 6 and I didn’t want to confuse you by changing it’s name here! If you find any mistake in my code please do inform me so that I can make necessary changes in my code. Please use Beautiful Code plugin if you find any errors other than logical errors! Thanks! 🙂

public class Palindrome {

public static void main(String[] args) {

int num = 121, reversedInteger = 0, remainder, originalInteger;

originalInteger = num;

// reversed integer is stored in variable

while( num != 0 )

{

remainder = num % 10;

reversedInteger = reversedInteger * 10 + remainder;

num /= 10;

}

// palindrome if orignalInteger and reversedInteger are equal

if (originalInteger == reversedInteger)

System.out.println(originalInteger + ” is a palindrome.”);

else

System.out.println(originalInteger + ” is not a palindrome.”);

}

}

import java.util.*;

public class Palindrome {

public static void main(String[] args) {

System.out.println(“Enter a string”);

Scanner input=new Scanner(System.in);

String str=input.nextLine();

int i,len,flag=0;

len=str.length();

for (i=0;i


Leave a Reply

Your email address will not be published.