You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* TODO Sample java program to test if a given string is a palindrome or not
*
*/
publicclassPalindromeTest {
Scannersc = newScanner(System.in);
StringenteredValue;
publicPalindromeTest(){
System.out.println("Please enter the string :");
enteredValue = sc.next(); // Taking the input from console
palindromeCheck(enteredValue); //Calling the method which checks if the given string is a palindrome or not
}
publicvoidpalindromeCheck(Stringvalue){
StringoriginalString = value;
intcounter =0;
for(inti=0;i<value.length();i++){ //Iterate the string
StringcurrentChar = value.substring(i,i+1);
if(!currentChar.equalsIgnoreCase(value.substring(value.length()-i-1,value.length()-i))){ //if starting letter and ending letter are not same increment the counter. Increment the positions further
counter++;
}
}
if(counter>0) //if counter is not zero it is not a palindrome else it is a palindrome
System.out.println("The given string "+"'"+enteredValue+"'"+" is not a palindrome");
else
System.out.println("The given string "+"'"+enteredValue+"'"+" is a palindrome");