Skip to content

Instantly share code, notes, and snippets.

@ox
Created October 18, 2011 18:05
Show Gist options
  • Save ox/1296165 to your computer and use it in GitHub Desktop.
Save ox/1296165 to your computer and use it in GitHub Desktop.
See if a word is a palindrome
// 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