Checking the String is palindrome or not in java
Dec 25, 2021
Java Program :
pallindromeDayOne.javapublic class pallindromeDayOne {
public static boolean checkPallindrome(String str){
boolean res=false;
if (str.length()==0||str.length()==1) return res;
int start = 0, end = str.length()-1;
while (start<end){
res= str.charAt(start) == str.charAt(end);
start++; end--;
}
return res;
}
public static void main(String[] args) {
System.out.println(checkPallindrome("abckcba")? "string is palindrome":"not palindrome" );
}
}
simplified and extended program :
public class pallindromeDayOne {
public static boolean checkPallindrome(String str){
boolean res=false;
if (str.length()==0||str.length()==1) return res;
int start = 0, end = str.length()-1;
while (start<end){
if (str.charAt(start) == str.charAt(end)) {
res = true;
} else {
res = false;
}
start++; end--;
}
return res;
}
public static void main(String[] args) {
if (checkPallindrome("abckcba")== true) {
System.out.println("string is palindrome");
} else {
System.out.println("not palindrome");
}
}
}