What is String Palindrome
Ans: If we reverse a given string and output of reversed string is exactly identical to our given string then our given String is called as Palindrome String.
Suppose String s1= MADAM String s2=reverse(s1)//reverse if s1==s2 then it a palindrome string else it is not a palindrome String Note: this is not a exact logic its just for understanding.
In this Topic we are going to take two examples.In Example-1 we will take Static Inputs(pre-defined Inputs). And in Example-2 we will take User Defined Inputs.
Algorithm/Program Explanation
- Start
- Take String s=”MADAM” as input
- StringBuffer revstr= new StringBuffer()// reverse string is stored here.
- Define For Loop
- for(int i=s.length()-1;i>=0;i–)
- revstr=revstr.append(s.charAt(i))
- Check for Condition
- if(s.equalsIgnoreCase(revstr.toString()))
- Print String is Palindrome
- else String is not a Palindrome
- End
Example-Taking Static Inputs
/*
Java Program to Check String Palindrome */
public class PalindromeString {
public static void main(String args[]){
String s="MADAM";
StringBuffer revstr= new StringBuffer();
for(int i=s.length()-1;i>=0;i--){
revstr=revstr.append(s.charAt(i));
}
if(s.equalsIgnoreCase(revstr.toString())){
System.out.println("String is Palindrome");
}
else{
System.out.println("String is not a Palindrome");
}
}
}
Output:
String is Palindrome
Example-2 Taking User Defined Inputs
/*
Java Program to Check String Palindrom */
import java.util.Scanner;
public class PalindromeString {
public static void main(String args[]){
System.out.println("Enter the String");
Scanner sc=new Scanner(System.in);
String s=sc.next();
StringBuffer revstr= new StringBuffer();
for(int i=s.length()-1;i>=0;i--){
revstr=revstr.append(s.charAt(i));
}
if(s.equalsIgnoreCase(revstr.toString())){
System.out.println("String is Palindrome");
}
else{
System.out.println("String is not a Palindrome");
}
}
}
Output:
Enter the String
dad
String is Palindrome
Related Topics
- Arithmetic Operations Java Program
- Java program to perform Logical and Relational operations
- Java program to Implement Mathematical functions defined in Java Math class
- Java basic Programs with Explanation
- Java Program to find Min,Max Element in an Array
- Java Program to sort Array Elements in ascending and descending order
- Java Program to display Array item at Even & Odd position
- Program to Calculate Sum and Average of Array elements
- Java Program to find Index of specific Element in an array
- Program to find common elements between two arrays
- Java Program to Add/Insert specific Element to an Array
- Java Program to delete specific element from an Array
- Program to calculate frequency of each element in Array
- Java Program to find Even and Odd Numbers in the arrays
- Java Program to search Given Number in an Array
- Java Program to Add or Sub Two Matrices
- Java Program to Multiply two or More Matrices
- Java Program to Print Array in Reverse Order
- Java Program To Transpose Given Matrix
- Java Programs List- Java Programs Examples With Output