RE: Programming challenge 3 in java. |
||
![]() ![]() Alpha Tr chiken The "ignore formatting and punctucations" threw me off for a minute and required me to write an extra function , but all in all it was a good refresher. // code start import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.lang.Character; public class p3 { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { String str; boolean b = false; BufferedReader in = new BufferedReader(new FileReader("somefile.txt")); while ((str = in.readLine()) != null) { b = isPalindrome(removeFormatting(str)); if(b) { System.out.println(str); } } in.close(); } public static String removeFormatting(String s) { String copy =""; for(int pos = 0; pos<=s.length()-1; pos++) { if(Character.isLetter(s.charAt(pos))) copy += s.charAt(pos); } return copy; } public static String reverseWord(String s) { String copyR = ""; for(int pos = s.length()-1; pos>=0; pos--) { copyR += s.charAt(pos); } return copyR; } public static boolean isPalindrome(String s) { boolean b = false; int i = 0; String reversedWord; reversedWord= reverseWord(s); // System.out.println(reversedWord); if (s.equalsIgnoreCase(reversedWord)) { b = true; } return b; } } // code end and the output matches your output. Next, i'll do it in scheme( for fun ) and *maybe* prolog. -chiken. Replies:
|
||
| CyberArmy::Forum v0.6 Generated In 0.02990 seconds |