You are here : Home / Core Java Tutorials / Interview Programs (beginner to advanced) in java / Level1 programs for (beginner)
In this core java programming tutorial we will write a program to find Palindrome- Find string is palindrome or not in java.
Q.What is palindrome?
A. If reverse of string is same as original one, than our string is palindrome or palindrome is a string which reads the same backward or forward.
Example in java>
input String : aabaa
output: it’s palindrome.
time complexity - will be O(n/2)
Full Program/SourceCode / Example to find Palindrome- Find string is palindrome or not in java >
/**
* @author AnkitMittal
*
*/
public class PalindromeExample {
public static void main(String...args){
String inputString="aabaa"; //String to be reversed
System.out.println(isPalindrome(inputString) ? inputString+ " is a palindrome." : inputString+ "is not a palindrome.");
}
/*
* returns true if inputString is palindrome.
*/
public static boolean isPalindrome(String inputString){
char ar[]=inputString.toCharArray();
for(int i=0,j=ar.length-1; i<(ar.length/2); i++,j--){
if(ar[i]!=ar[j])
return false;
}
return true;
}
}
/*OUTPUT
aabaa is a palindrome.
*/
|
So in this core java programming tutorial we wrote a program to - find Palindrome- Find string is palindrome or not in java.
Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.
RELATED LINKS>
Write a program that prints all the palindrome till 1000 in java
Labels:
Core Java
Level1 programs (beginners)