Created
October 18, 2011 18:05
-
-
Save ox/1296165 to your computer and use it in GitHub Desktop.
See if a word is a palindrome
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// compile with: | |
// javac Palindrome.java | |
// run it with: | |
// java Palindrome word | |
// where word is a word of your choosing | |
public class Palindrome { | |
public static boolean is_palindrome(String s) { | |
// if s is an empty string or s is a single character, return true | |
if(s == "" || s.length() == 1) return true; | |
// else if the first character is the same as the last character | |
else if(s.charAt(0) == s.charAt(s.length()-1)) | |
// return whether the letters in between the first and last letter are also a palindrome | |
return is_palindrome(s.substring(1, s.length() - 1)); | |
// if the first and last characters are not the same, then this is not a palindrome | |
else return false; | |
} | |
public static void main(String[] args) { | |
// if the first argument to java Palindrome is a palindrome, print 'true' | |
if(is_palindrome(args[0])) | |
System.out.println("true"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment