Skip to content

Instantly share code, notes, and snippets.

@bradjohansen
Created August 8, 2010 01:58
Show Gist options
  • Save bradjohansen/513461 to your computer and use it in GitHub Desktop.
Save bradjohansen/513461 to your computer and use it in GitHub Desktop.
A Palindrome Tester in Java
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();
}
}
@bryantwilliam
Copy link

It cool :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment