Difference between revisions of "Week 5"
Line 102: | Line 102: | ||
What is the length of the string, str = "apple"? It's 5.<br/> | What is the length of the string, str = "apple"? It's 5.<br/> | ||
What is the index of the last character e in the string? It's 4!!<br/> | What is the index of the last character e in the string? It's 4!!<br/> | ||
+ | <br/> | ||
If you try to get a character at str.charAt(5), then you will get run-time errors.<br/> | If you try to get a character at str.charAt(5), then you will get run-time errors.<br/> | ||
This is very important to know and you will be facing many variations of these errors later.<br/> | This is very important to know and you will be facing many variations of these errors later.<br/> |
Revision as of 15:34, 5 April 2010
Back to the Program-A-Day homepage
ProblemThis question will help you practice with some 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
An image or By Students section | |
---|---|---|
SolutionFirst of all, you will need to declare two string objects to get 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 |