String palindrome in Java
In this post, you will learn how to write a perfect string palindrome program using the Java programming language.
A palindrome is a number, string, or sequence that remains the same when it is reversed. It can be a combination of characters or digits, but it looks the same going forward and backward. It is generally used where recreational mathematics is applied, like puzzles and games. To check whether a string is a palindrome or not, simply reverse it and compare it with the original string. If both are the same, then the string is a palindrome, otherwise not.
These are different programming ways in Java to check a string palindrome.
MAAM, DAD, WOW, TENET, MADAM
When we reverse the above strings, they will remain the same. So, all the above are palindromes.
Java program to check string palindrome using for loop
In the given Java program, we use the for loop to check palindromes. Within this, we reverse the string and compare it with the original string. The for loop runs from the end to the beginning of the given string and accesses each character of the string using the charAt() method and stores it in the y variable. The equalsIgnoreCase() method determines whether two strings are equal.
import java.util.Scanner;
public class StringPalindrome
{
public static void main(String args[])
{
String x, y = "";
Scanner sc =new Scanner(System.in);
// getting input from user
System.out.print("Enter a sentence: ");
x = sc.nextLine();
// getting length of string
int len = x.length();
for(int k = len - 1; k >= 0; k--)
{
y = y + x.charAt(k);
}
if(x.equalsIgnoreCase(y))
{
System.out.println("The string "+ x +" is palindrome.");
}
else
{
System. out.println("The string "+ x +" is not a palindrome.");
}
}
}
Output of the above code:
Enter a sentence: MADAM
The string MADAM is palindrome.
Enter a sentence: DAD
The string DAD is palindrome.
Java program to check string palindrome using while loop
In the given Java program, we ask the user to enter sentence and store them in a variable 'str'. Then, we use the while loop to extract the word and reverse the string.
import java.util.Scanner;
public class StringPalindrome
{
public static void main(String[] args) {
// Creating an object for Scanner
Scanner sc = new Scanner(System.in);
// Getting input from user
System.out.print("Enter a string: ");
String str = sc.nextLine();
String revStr="";
int i;
//Get the length of string
int strLen = str.length();
i = strLen - 1;
// loop through it and print it turn around
while(i >= 0){
revStr = revStr + str.charAt(i);
i--;
}
//Check weather entered string equals to reverse string
if(str.equals(revStr)){
//If Yes, it is a palindrom string
System.out.println(str+" is a palindrome string.");
}else{
//If No, it is not a palindrom string
System.out.println(str+" is not a palindrome string.");
}
}
}
Output of the above code:
Enter a sentence: MADAM
The string MADAM is palindrome.
Enter a sentence: DAD
The string DAD is palindrome.
Java program to check string palindrome using Library functions
In the given Java program, we ask the user to enter sentence and store them in a variable 'str'. Then, we reverse the string with the help of library function and check whether the entered string equals to the reverse string or not.
import java.util.Scanner;
public class StringPalindrome
{
public static void main(String[] args) {
// Creating an object for Scanner
Scanner sc = new Scanner(System.in);
// Getting input from user
System.out.print("Enter a string: ");
String str = sc.nextLine();
//Reversing the string
String revStr = new StringBuffer(str).reverse().toString();
//Check weather entered string equals to reverse string
if(str.equals(revStr)){
System.out.println("The string "+ str +" is palindrome.");
}else{
System.out.println("The string "+ str +" is not a palindrome.");
}
}
}
Output of the above code:
Enter a string: RADAR
The string RADAR is palindrome.
Enter a string: COBBLER
The string COBBLER is not a palindrome.
Related Articles
Number pattern programs in JavaJava program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number