Difference between revisions of "Week 5"
(7 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
|Problem= | |Problem= | ||
− | This question will help you practice with some string methods and for loops. | + | This question will help you practice with some string methods and for loops.<br/> |
− | + | <br/> | |
Complete the program Palindrom that checks if a word is palindrome.<br/> | Complete the program Palindrom that checks if a word is palindrome.<br/> | ||
If the word is palindrome, print out "It's a palindrome." using System.out.println().<br/> | If the word is palindrome, print out "It's a palindrome." using System.out.println().<br/> | ||
Line 64: | Line 64: | ||
|Solution= | |Solution= | ||
− | |||
− | |||
− | |||
+ | First of all, you will need to declare two string objects to get reversed words. <br/> | ||
+ | You will need to INITIALIZE the strings, which means make the strings empty. <br/> | ||
+ | <br/> | ||
+ | <pre> | ||
String reverse1 = "" | String reverse1 = "" | ||
String reverse2 = "" | String reverse2 = "" | ||
− | + | </pre> | |
− | Then, you will need first for loop to reverse the first word. This loop will start from 0 and finishes looping when i >= testWord1.length. | + | <br/> |
− | The increment will be 1 since we are getting each character from the string. | + | Then, you will need first for loop to reverse the first word. This loop will start from 0 and finishes looping when i >= testWord1.length.<br/> |
− | + | The increment will be 1 since we are getting each character from the string.<br/> | |
+ | <br/> | ||
+ | <pre> | ||
for(int i = 0; i < testWord1.length(); i++) | for(int i = 0; i < testWord1.length(); i++) | ||
{ | { | ||
} | } | ||
− | + | </pre> | |
− | Once you have the for loop set up, you will need to go through each letter of the string, using charAt(i) and concatenate the letters in reverse order. | + | <br/> |
− | The peusdo-code below concatenates each letter of the string in reversed order. | + | Once you have the for loop set up, you will need to go through each letter of the string, using charAt(i) and concatenate the letters in reverse order.<br/> |
− | + | The peusdo-code below concatenates each letter of the string in reversed order.<br/> | |
+ | <br/> | ||
+ | <pre> | ||
reverse1 = get the letters of the str by charAt(i) + reverse1 | reverse1 = get the letters of the str by charAt(i) + reverse1 | ||
− | + | </pre> | |
− | Then, you will need another for loop to reverse testWord2. This time, we will use some other approach and make the for loop start from the end. | + | <br/> |
− | It's strongly recommended to try this way, because you will have better idea how the for loop works. | + | Then, you will need another for loop to reverse testWord2. This time, we will use some other approach and make the for loop start from the end.<br/> |
− | + | It's strongly recommended to try this way, because you will have better idea how the for loop works.<br/> | |
+ | <br/> | ||
+ | <pre> | ||
for(int i = testWord2.length()-1; i >= 0; i--) | for(int i = testWord2.length()-1; i >= 0; i--) | ||
{ | { | ||
} | } | ||
− | + | </pre> | |
− | The code above starts from the length of the testWord2 - 1, ends when i becomes less than 0. Why does it have to start from the string length - 1? | + | <br/> |
− | It's because there is no character at the length of the string. See the example below. | + | The code above starts from the length of the testWord2 - 1, ends when i becomes less than 0. Why does it have to start from the string length - 1?<br/> |
− | What is the length of the string, str = "apple"? It's 5. | + | It's because there is no character at the length of the string. See the example below.<br/> |
− | What is the index of the last character e in the string? It's 4!! | + | What is the length of the string, str = "apple"? It's 5.<br/> |
− | If you try to get a character at str.charAt(5), then you will get errors. | + | What is the index of the last character e in the string? It's 4!!<br/> |
− | This is very important to know and you will be facing many variations of these errors later. | + | <br/> |
− | + | If you try to get a character at str.charAt(5), then you will get run-time errors.<br/> | |
− | Now, back to the question. | + | This is very important to know and you will be facing many variations of these errors later.<br/> |
− | We have to decrement it by 1 now, because we are getting from the end character of the string. | + | <br/> |
− | How would concatenation work now? | + | Now, back to the question.<br/> |
− | + | We have to decrement it by 1 now, because we are getting from the end character of the string.<br/> | |
+ | How would concatenation work now?<br/> | ||
+ | <br/> | ||
+ | <pre> | ||
reverse2 = reverse2 + letters of the str by charAt(i) | reverse2 = reverse2 + letters of the str by charAt(i) | ||
− | + | </pre> | |
− | str = "abcde" | + | str = "abcde"<br/> |
− | + | <br/> | |
− | reverse = reverse + charAt(i) of str from end would be | + | reverse = reverse + charAt(i) of str from end would be<br/> |
− | e | + | e<br/> |
− | ed | + | ed<br/> |
− | edc | + | edc<br/> |
− | edcb | + | edcb<br/> |
− | edcba | + | edcba<br/> |
− | + | <br/> | |
− | Once you have the two words reversed, you will need a condition statement to check if they are palindromes. | + | Once you have the two words reversed, you will need a condition statement to check if they are palindromes.<br/> |
+ | <br/> | ||
+ | <pre> | ||
if testWord == reversedTestWord | if testWord == reversedTestWord | ||
{ | { | ||
Line 121: | Line 133: | ||
print out reservedTestWord | print out reservedTestWord | ||
} | } | ||
− | + | </pre> | |
− | One thing to note is, strings cannot be compared using '==' sings. | + | <br/> |
− | You will need a special method called testWord.equals(reversedTestWord). | + | One thing to note is, strings ''cannot'' be compared using '==' sings.<br/> |
− | If you want to check without matching cases, use str.equalsIgnoreCase() | + | You will need a special method called:<br/> |
− | + | <br/> | |
+ | <pre> | ||
+ | testWord.equals(reversedTestWord). | ||
+ | </pre> | ||
+ | <br/> | ||
+ | * If you want to check without matching cases, use | ||
+ | <pre> | ||
+ | str.equalsIgnoreCase() | ||
+ | </pre> | ||
+ | <br/> | ||
+ | See below for the solution code. | ||
}} | }} |
Latest revision as of 15:38, 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 |