This program will be ran through the Eclipse IDE using Java
This program will be ran through the Eclipse IDE using Java (JDK8)
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 (all lowercase without spaces) 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() rightarrow 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: sorewasiereilsaweros neverafoottoofareven noelseesleon nomistsorfrostsimon User entered: toohottohoot This is a Palindrome! User entered: thisissomething This is NOT a Palindrome!Solution
Please find my code:
import java.util.Scanner;
public class Palindrome {
public static boolean isPalindrome(String str){
int i=0;
int j = str.length()-1;
// traversing from front and last and comparing corresponding character
while(i < j){
if(str.charAt(i) != str.charAt(j))
return false;
i++;
j--;
}
return true; // reach here: means string is palindrome
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(\"Enter string: \");
String str = sc.nextLine();
// calling method
boolean status = isPalindrome(str);
if(status)
System.out.println(\"This is a Palindrome\");
else
System.out.println(\"This is not a Palindrome\");
}
}
/*
Sample Output:
Enter string:
thisiht
This is a Palindrome
*/

