Java Palindrome Program


Java Palindrome Program

This Java palindrome program lets the user enter any positive integer and then it will check whether the number is a palindrome or not using the while loop.

In this article, I have explained the list of all alphabet pattern programs in c programming language. Here, I have used the while loop to iterate each and every character of the string and checked for palindrome string.

A Java program for checking if a given string is a palindrome or not. A palindrome is a word that reads the same backward as forward. Examples of palindromes are madam, civic, radar, level, rotator, and kayak.

import java.util.Scanner;

public class Palindrome {

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 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 is a palindrome.”);

else

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

}

import java.util.*;

public class PalindromeExample

{

public static void main(String[] args)

{

System.out.println(“Please Enter the String for check:”);

Scanner in = new Scanner(System.in);

String inputString = in.nextLine();

boolean result = isPalindrome(inputString);

if (result) {

System.out.println(“Given String is a palindrome”);

} else {

System.out.println(“Given String is not a palindrome”);

}

}

public static boolean isPalindrome(String input) { char[] try1= input.toCharArray(); int i1 = 0; int i2 = try1.length – 1; while (i2 > i1) { if (try1[i1] != try1[i2]) { return false; } ++i1; –i2; } return true; } }

import java.util.*;

class PalindromeI

{

public static void main(String args[])

{

String a, b = “”;

Scanner s = new Scanner(System.in);

System.out.print(“Enter the string you want to check:”);

a = s.nextLine();

int n = a.length();

for(int i = n – 1; i >= 0; i–)

{

b = b + a.charAt(i);

}

if(a.equalsIgnoreCase(b))

{

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

}

else

{

System.out.println(“The string is not palindrome.”);*

import java.io.*;

public class Palindrome

{

public static void main(String args[])throws IOException

{

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String str, rev = “”;

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

str = br.readLine();

int length = str.length();

for (int i = length – 1; i >= 0; i–)

rev = rev + str.charAt(i);

if (str.equals(rev))

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

else

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

}

}

import java.io.*;

class palindrome {

public static void main(String args[]) throws IOException {

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(isr);

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

String s = br.readLine();

int size = s.length();

for (int i = 0; i < size / 2; ++i) { if (s.charAt(i) != s.charAt(size - 1 - i)) { System.out.println("Not a palindrome."); return; } } System.out.println("Palindrome."); } } import java.util.*; class Palindrome { public static void main(String args[]) { Scanner in=new Scanner(System.in); System.out.println("Enter a string"); original=in.nextLine(); int length = original.length(); for ( int i = length - 1; i >= 0; i– )

rev = rev + original.charAt(i);

if (original.equals(rev))

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

else

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


Leave a Reply

Your email address will not be published.