Created
August 8, 2010 01:58
-
-
Save bradjohansen/513461 to your computer and use it in GitHub Desktop.
A Palindrome Tester in Java
This file contains 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
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
/** | |
* Created by IntelliJ IDEA. | |
* User: Brad | |
* Date: Aug 7, 2010 | |
* Time: 8:41:46 PM | |
*/ | |
public class Palindrome { | |
public static void main(String[] args) { | |
System.out.println("Let's test for palindromes!\n"); | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
String input = null; | |
try { | |
System.out.println("Type a word: "); | |
input = br.readLine(); | |
} catch (IOException ioe) { | |
System.out.println("IO Error trying to get input!"); | |
System.exit(1); | |
} | |
System.out.println("Your word: " + input); | |
test(input); | |
} | |
public static void test(String input) { | |
if (input.equals(ReverseString.reverse(input))) { | |
System.out.println(input + " is a palindrome!"); | |
} else { | |
System.out.println(input + " is not a palindrome..."); | |
} | |
} | |
} | |
class ReverseString { | |
public static String reverse(String source) { | |
int i, len = source.length(); | |
StringBuffer dest = new StringBuffer(len); | |
for (i = (len - 1); i >= 0; i--) { | |
dest.append(source.charAt(i)); | |
} | |
return dest.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It cool :D