Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by Foxes on 10/29/2014.
- *
- * For Anoni :3
- */
- import java.util.Scanner;
- public class Palindromes {
- public static final String allowed = "abcdefghijklmnopqrstuvwxyz1234567890";
- public static void main(String[] args) {
- String original = "", smashed = "";
- boolean bAskForText = (args.length == 0);
- for (int word = 0; word < args.length; ++word) {
- if (word == 0) original = args[0];
- else {
- original = original + " " + args[word];
- }
- }
- Scanner in = new Scanner(System. in );
- for (;;) {
- if (bAskForText) {
- System.out.print("\n\nEnter text to check: ");
- original = in .nextLine();
- } else bAskForText = true;
- smashed = original.toLowerCase();
- if (smashed.contentEquals("quit")) System.exit(0);
- int palinSz = 1;
- String found = "";
- for (int start = 0;
- (start + palinSz) < smashed.length(); ++start) {
- for (int last = (start + palinSz); last < smashed.length(); ++last) {
- String testStr = smashed.substring(start, last + 1);
- if (testStr.length() > 1 && isPalin(testStr)) {
- found = testStr;
- palinSz = found.length();
- }
- }
- }
- if (found.length() > 0) {
- System.out.println(" largest found: " + found);
- } else {
- System.out.println(" no palindromes found.");
- }
- }
- }
- private static boolean isPalin(String str) {
- for (int i = 0; i < (str.length() / 2); ++i) {
- int j = str.length() - (i + 1);
- if (str.charAt(i) != str.charAt(j)) {
- dbg(" -:" + str);
- return false;
- }
- if (!allowed.contains(Character.toString(str.charAt(i)))) {
- dbg(" -:" + str);
- return false;
- }
- }
- dbg(" **:" + str);
- return true;
- }
- private static void dbg(String s) {
- boolean display = false;
- if (display) {
- System.out.println(s);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment