File:Execute.java
---------------------------------------------------------------------------------
package org.developersbrain.solutions;
public class Execute {
public static boolean isPalindrome(String str){
char[] chrArr=str.toCharArray();
int len=chrArr.length;
boolean palindrome=true;
for(int i=0,j=len-1;i<=len/2;i++,j--){
if(chrArr[i]!=chrArr[j]){
palindrome=false;
}
}
return palindrome;
}
public static void main(String[] args){
String[] str={"MADAM","CIVIC","SINGLE","DONKEY","RADAR"};
for(String strV:str){
if(isPalindrome(strV)){
System.out.println("String \""+strV+"\" is palindrome.");
}else{
System.out.println("String \""+strV+"\" is not palindrome.");
}
}
}
}
---------------------------------------------------------------------------------
package org.developersbrain.solutions;
public class Execute {
public static boolean isPalindrome(String str){
char[] chrArr=str.toCharArray();
int len=chrArr.length;
boolean palindrome=true;
for(int i=0,j=len-1;i<=len/2;i++,j--){
if(chrArr[i]!=chrArr[j]){
palindrome=false;
}
}
return palindrome;
}
public static void main(String[] args){
String[] str={"MADAM","CIVIC","SINGLE","DONKEY","RADAR"};
for(String strV:str){
if(isPalindrome(strV)){
System.out.println("String \""+strV+"\" is palindrome.");
}else{
System.out.println("String \""+strV+"\" is not palindrome.");
}
}
}
}
Output:
--------------------------------------------------------------------------------
String "MADAM" is palindrome.
String "CIVIC" is palindrome.
String "SINGLE" is not palindrome.
String "DONKEY" is not palindrome.
String "RADAR" is palindrome.
Comments
Post a Comment