A Palindrome is a word or phrase that is spelled SAME forwar
A Palindrome is a word or phrase that is spelled SAME forwards and backwards.
e.g.: too hot to hoot
Write a Java program that reads a phrase from the console and determines if it is a Palindrome or not.
You can use a Scanner method nextLine() to read a line from the console. You can assume user will enter all lower case phrase.
You can find out the length of the phrase using the String method \"length(). s.length() -> gives the length of string s.
You need to put the phrase user entered into a Char Array. You can do this using the charAt(n) method of String.
Example: c[0] = \"abc\".charAt(1); - this will place \'b\' in char array c at [0].
You can test your program with the following Plaindromes:
sore was I ere I saw eros
never a foot too far even
noel sees leon
no mists or frost simon
--------
Sample runs:
(1) User entered: too hot to hoot This is a Palindrome!
(2) User entered: this is something
This is NOT a Palindrome!
----------
Solution
import java.util.*;
public class chekpallendrome {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println(\"enter line of text\");
String input=sc.nextLine();
String res=\"\";
String output=\"\";
for(int i=0;i<input.length();i++){
if(input.charAt(i)!=\' \'){
res=res+input.charAt(i);
}
}
for(int i=res.length()-1;i>=0;i--)
output=output+res.charAt(i);
if(res.equals(output))
System.out.println(\"this is pallendrome\");
else
System.out.println(\"NOT a pallendrome\");
}
}
