In: Computer Science
Java Palindrome
Use StringBuilder concept to create a palindrome checker.
Method should be called pCheck.
Must use var-arg concept.
Class name for this program is Palindrome.
Must pass this test::
public class PalindromeTest {
public static void main(String[] args) {
Palindrome palindrome = new Palindrome();
boolean answer = palindrome.pCheck("racecar", "Bell", "elle", "bunny");
System.out.println(“These are palindromes: " + answer);
}
}
If you have any doubts, please give me comment...
Here I used if all the strings are palindromes, then pCheck method returns true otherwise returns false.
class Palindrome{
public boolean pCheck(String...strings){
for(int i=0; i<strings.length; i++){
int len = strings[i].length();
for(int j=0; j<len/2; j++){
if(strings[i].charAt(j)!=strings[i].charAt(len-j-1))
return false;
}
}
return true;
}
}
public class PalindromeTest {
public static void main(String[] args) {
Palindrome palindrome = new Palindrome();
boolean answer = palindrome.pCheck("racecar", "Bell", "elle", "bunny");
System.out.println("These are palindromes: " + answer);
}
}
Let me know if any output differ... Thank you...