write a palindrome check for a sentence in java i was able to write a palindrome
ID: 644286 • Letter: W
Question
write a palindrome check for a sentence in java
i was able to write a palindrome check for a word but still unable to input a whole sentence
the test output should be
"A Santa as NASA" is NOT a palindrome
"Acrobats stab orca" is a palindrome
but when i insert "A Santa as NASA" or "Acrobats stab orca", all i got is
"A" is not a is NOT a palindrome
or "Acrobats" is NOT a palindrome
Here is the code
-------------------------------------------------------------------------------
import java.util.*;
public class pen {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
printpallindrom(input);
}
public static void printpallindrom(Scanner input) {
System.out.println("Enter");
String text = input.nextLine();
text = text.replaceAll("[^a-zA-Z]+","").toLowerCase();
boolean status = true;
for (int i = 0; i < text.length() / 2; i++) {
if (text.charAt(i) != text.charAt(text.length() - i - 1)) {
status = false;
break;
}
}
if (status) {
System.out.println(text + " is a palindrome");
}
else {
System.out.println(text + " is not a palindrome");
}
}
}
Explanation / Answer
------------------------------------------------------------------------------------------------------------------------------
simple method:
String s1,s2;
assign a string to s1;
if( Strrev(s1)==s2)
print "palindrome"
else
print " not palindrome"
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.