Difference between revisions of "Palindromes"
m |
|||
Line 33: | Line 33: | ||
|SideSection= | |SideSection= | ||
− | You will often confront many errors when you are programming. There are two types of errors: run-time error or compiler error. If you have compiler errors, you will have to go through the code and check carefully if you did not make mistakes anywhere. However, run-time errors are harder to detect and fix. For first year computer science level, most of the run-time errors will be caused by wrong index subscription. | + | You will often confront many errors when you are programming. There are two types of errors: run-time error or compiler error. If you have compiler errors, you will have to go through the code and check carefully if you did not make mistakes anywhere. However, run-time errors are harder to detect and fix. For first year computer science level, most of the run-time errors will be caused by wrong index subscription. This example will show you some run-time errors that can be caused by wrong index subscription. |
|SolutionCode= | |SolutionCode= |
Revision as of 18:30, 10 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 } } |
By students..You will often confront many errors when you are programming. There are two types of errors: run-time error or compiler error. If you have compiler errors, you will have to go through the code and check carefully if you did not make mistakes anywhere. However, run-time errors are harder to detect and fix. For first year computer science level, most of the run-time errors will be caused by wrong index subscription. This example will show you some run-time errors that can be caused by wrong index subscription. | |
---|---|---|
SolutionFirst of all, you will need to declare two string objects to store the reversed words. String reverse1 = "" String reverse2 = ""
str.length();
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" Once you have the two words reversed, you will need a condition statement to check if they are palindromes.
if testWord == reversedTestWord { Then, print out success } else { print out reservedTestWord }
testWord.equals(reversedTestWord).
str.equalsIgnoreCase()
| ||
Code |