CyberArmy University | Open Source Institute | CyberArmy Intelligence & Security | CyberArmy Services & Projects

RE: Programming challenge 3 in java.


[Replies] [Reply] [View by Thread] [Help]
[Back To Programming]

Posted by Alpha Tr chiken On 2008-07-21 00:54:35
In Reply to Programming challenge 3 -easy- Posted by Delta Cpt dopel On 2008-07-20 23:35:30

Alpha TrAlpha Tr
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:


Guest:
Subject:
Message:
Signature:
Optional Image Link:
http://

CyberArmy::Forum v0.6
Generated In 0.02990 seconds


About Us | Privacy Policy | Mission Statement | Help