Palindrome String in Java
By: Grant Braught
Checking if a String is a Palindrome is a common assignment given to Java students. This sample class is a helpful class that checks for various scenarios to find out if the given string is a palindrome.public class Palindrome {
private String pal;
public Palindrome(String initPal) {
pal = initPal.toUpperCase();
}
public boolean isPalindrome() {
if (pal.length() <= 1) {
// String has only one character so it
// is a Palindrome by definition.
return true; // BASE CASE.
}
// Get the first and last characters of the String.
char first = pal.charAt(0);
char last = pal.charAt(pal.length()-1);
if (Character.isLetter(first) &&
Character.isLetter(last)) {
// The first and last characters are both letters..
if (first != last) {
// The first and last letters are different
// so the string is not a Palindrome.
return false; // BASE CASE.
}
else {
// The first and last characters are both letters,
// and they are both the same. So, the string is
// a palindrome if the substring created by dropping
// the first and last characters is a palindrome.
Palindrome sub = new Palindrome(
pal.substring(1,pal.length()-1));
return sub.isPalindrome(); // RECURSIVE CASE.
}
}
else if (!Character.isLetter(first)) {
// The first character in the string is not a letter.
// So the string is a palindrome if the substring created
// by dropping the first character is a palindrome.
Palindrome sub = new Palindrome(pal.substring(1));
return sub.isPalindrome(); // RECURSIVE CASE.
}
else {
// The last character in the string is not a letter.
// So the string is a palindrome if the substring created
// by dropping the last character is a palindrome.
Palindrome sub = new Palindrome(
pal.substring(0,pal.length()-1));
return sub.isPalindrome(); // RECURSIVE CASE.
}
}
public static void main(String[] args) {
Palindrome p1 = new Palindrome("Madam, I'm Adam.");
System.out.println(p1.isPalindrome());
Palindrome p2 = new Palindrome("Nope!");
System.out.println(p2.isPalindrome());
Palindrome p3 = new Palindrome("dad");
System.out.println(p3.isPalindrome());
Palindrome p4 = new Palindrome("Go hang a salami, I'm a lasagna hog.");
System.out.println(p4.isPalindrome());
}
}
Archived Comments
1. DavidFaf
View Tutorial By: DavidFaf at 2016-11-27 18:33:29
2. import java.util.*;
class Pstring
{
public static void main(String
View Tutorial By: Bhaskar.pg at 2014-05-29 12:39:52
3. this works
------------------------------------------
import java.util.Scanne
View Tutorial By: Aeron at 2013-10-20 04:50:51
4. simple way friends:
public class palindrome {
public static void main(String[] args)
View Tutorial By: Manoj at 2013-01-07 02:18:00
5. Why use recursion in Java, even for Strings? The stack will grow and use more time and space. If we
View Tutorial By: Kristoffer L at 2012-11-21 01:51:36
6. @Palla Subramanyam : Your logic is correct, but SOP is not proper.
if(abc.equals(def)
View Tutorial By: Roopesh H at 2012-07-27 04:47:48
7. The smiple easy and best way is :)
------------------------------------------------
im
View Tutorial By: Sandhya at 2012-06-21 06:08:25
8. This should save some time
class palindrome
{
public static void main(S
View Tutorial By: vamsi gude at 2012-03-20 18:27:24
9. The one more best way is
public class Palindrome {
static public String pal(
View Tutorial By: Palla Subramanyam at 2012-01-25 20:18:03
10. the discussion is not enogh
View Tutorial By: gulstan at 2011-03-27 03:47:24
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java