Revisions
-
iboB revised this gist
Nov 14, 2011 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,3 @@ public class PalindromeChecker { public static void main(String[] args) { String[] sWordsToCheck = new String[] { -
iboB revised this gist
Nov 14, 2011 . No changes.There are no files selected for viewing
-
iboB revised this gist
Nov 14, 2011 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,9 @@ public class PalindromeChecker { public static void main(String[] args) { String[] sWordsToCheck = new String[] { "gatemannametag", //p "abcdefghijklmnopqrtuvwxyz", // not p "aaaaa1aaaaa", //p "aaaaa12aaaaa", // not p }; for(int i=0; i<sWordsToCheck.length; ++i) { -
iboB revised this gist
Nov 14, 2011 . 1 changed file with 29 additions and 24 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,29 +1,33 @@ package hellojava; public class PalindromeChecker { public static void main(String[] args) { String[] sWordsToCheck = new String[] { "gatemannametag", //p "abcdefghijklmnopqrtuvwxyz" // not p }; for(int i=0; i<sWordsToCheck.length; ++i) { String sWordToCheck = sWordsToCheck[i]; System.out.print("palindrome1: "); if(isPalindrome1(sWordToCheck)) System.out.println(sWordToCheck + " Is a Palindrome "); else System.out.println(sWordToCheck + " Is Not a Palindrome "); System.out.print("palindrome2: "); if(isPalindrome2(sWordToCheck)) System.out.println(sWordToCheck + " Is a Palindrome "); else System.out.println(sWordToCheck + " Is Not a Palindrome "); System.out.print("palindrome3: "); if(isPalindrome2(sWordToCheck)) System.out.println(sWordToCheck + " Is a Palindrome "); else System.out.println(sWordToCheck + " Is Not a Palindrome "); } } @@ -49,6 +53,7 @@ public static boolean isPalindrome1(String word) { public static boolean isPalindrome2(String sWord) { int len = sWord.length(); int halfLen = sWord.length()/2; for (int i = 0; i < halfLen; i++) { if (sWord.charAt(i) != sWord.charAt(len - i - 1)) { @@ -58,4 +63,4 @@ public static boolean isPalindrome2(String sWord) { return true; } } -
iboB revised this gist
Nov 14, 2011 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -49,8 +49,8 @@ public static boolean isPalindrome1(String word) { public static boolean isPalindrome2(String sWord) { int halfLen = sWord.length()/2; for (int i = 0; i < halfLen; i++) { if (sWord.charAt(i) != sWord.charAt(len - i - 1)) { return false; } -
josescalia created this gist
Nov 14, 2011 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,61 @@ public class PalindromeChecker { public static void main(String[] args) { String sWordToCheck = "malam"; System.out.println("Using palindrome1 : "); if(isPalindrome1(sWordToCheck)) System.out.println(sWordToCheck + " Is Palindrome "); else System.out.println(sWordToCheck + " Is Not Palindrome "); System.out.println("\n"); System.out.println("Using palindrome2 : "); if(isPalindrome2(sWordToCheck)) System.out.println(sWordToCheck + " Is Palindrome "); else System.out.println(sWordToCheck + " Is Not Palindrome "); System.out.println("\n"); System.out.println("Using palindrome3 : "); if(isPalindrome2(sWordToCheck)) System.out.println(sWordToCheck + " Is Palindrome "); else System.out.println(sWordToCheck + " Is Not Palindrome "); } public static boolean isPalindrome3(String sWordToCheck) { return sWordToCheck.equals(new StringBuffer(sWordToCheck).reverse().toString()); } public static boolean isPalindrome1(String word) { int left = 0; // index of leftmost unchecked char int right = word.length() - 1; // index of the rightmost while (left < right) { // continue until they reach center if (word.charAt(left) != word.charAt(right)) { return false; // if chars are different, finished } left++; // move left index toward the center right--; // move right index toward the center } return true; // if finished, all chars were same } public static boolean isPalindrome2(String sWord) { int len = sWord.length(); for (int i = 0; i < (len % 2); i++) { if (sWord.charAt(i) != sWord.charAt(len - i - 1)) { return false; } } return true; } }