Difference between revisions of "Palindromes"
(Fixed typos, reworded numerous sentences, fixed formatting, etc, etc. My eyes hurt.) |
m (Removed pre tages in Code section) |
||
Line 34: | Line 34: | ||
<BR> | <BR> | ||
− | |SolutionCode= | + | |SolutionCode= |
String reversedWord1 = ""; | String reversedWord1 = ""; | ||
String reversedWord2 = ""; | String reversedWord2 = ""; | ||
Line 57: | Line 57: | ||
System.out.println(reversedWord1); | System.out.println(reversedWord1); | ||
} | } | ||
− | |||
− | |||
|Solution= | |Solution= |
Revision as of 19:41, 8 April 2010
Back to the Program-A-Day homepage
ProblemThis question will help you practice with string methods and for loops. public class Palindrome { public static void main(String[] args) { String testWord1 = "Arewenotdrawnonward,wefew,drawnonwardtonewera" String testWord2 = "Murderforajarofredrum" // Your code goes here } } |
Palindromes
| |
---|---|---|
SolutionFirst of all, you will need to declare two string objects to store the reversed words. String reverse1 = "" String reverse2 = ""
for(int i = 0; i < testWord1.length(); i++) { }
reverse1 = get the letters of the str by charAt(i) + reverse1
for(int i = testWord2.length()-1; i >= 0; i--) { }
reverse2 = reverse2 + letters of the str by charAt(i) str = "abcde" if testWord == reversedTestWord { Then, print out success } else { print out reservedTestWord }
testWord.equals(reversedTestWord).
str.equalsIgnoreCase()
| ||
Code |