Palindrome in Java: Java program to check if a string is a palindrome or not. A string is a palindrome on the off chance that it stays unaltered when switched, for instance, "father" is a palindrome as turn around of "father" is "father" though "program" isn't a palindrome as its invert seems to be "margorp". Some different cases of palindrome strings are "mother", "madam", "abcba", "12321", "c++&++c". In our program client will include a string and we make another string which will be its turn around and after that we contrast it and the entered string.
Palindrome program in Java
import java.util.*;
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 isn't a palindrome.");
}
}
Java palindrome program without switching a string
Another technique to check palindrome: We can think about characters toward the start and toward the finish of a string and move towards the center of the string.
import java.util.*;
class Palindrome
{
public static void main(String args[])
{
String inputString;
Scanner in = new Scanner(System.in);
System.out.println("Input a string");
inputString = in.nextLine();
int length = inputString.length();
int i, begin, end, middle;
begin = 0;
end = length - 1;
middle = (begin + end)/2;
for (i = begin; i <= middle; i++) {
if (inputString.charAt(begin) == inputString.charAt(end)) {
begin++;
end--;
}
else {
break;
}
}
if (i == middle + 1) {
System.out.println("Palindrome");
}
else {
System.out.println("Not a palindrome");
}
}
}
Both the above codes think about strings as case delicate, you can change them with the goal that they disregard the instance of the string. You can either change over the two strings to lower or capitalized for this. Make an effort not to change the first strings as they might be additionally required in the program.
Java Program to Check whether a String is a Palindrome
This is a Java Program to Check whether a String is a Palindrome.
Enter any string as information. Presently we use for circles and if-else conditions alongside equalsIgnoreCase() technique to finish up whether the entered string is palindrome or not.
Here is the source code of the Java Program to Check whether a String is a Palindrome. The Java program is effectively arranged and kept running on a Windows framework. The program yield is additionally demonstrated as follows.
public class Palindrome
{
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.");
}
}
}
0 comments:
Post a Comment