Difference between revisions of "Palindromes"

From CompSciWiki
Jump to: navigation, search
Line 37: Line 37:
 
Second, take a break if you are stuck. If you are mentally tired and cannot figure a way out, take a break. Otherwise, you will even mess up with the parts that were actually correct. This will make you extremely stressed out, and you will most likely <i>NOT</i> finish your assignment.<br>
 
Second, take a break if you are stuck. If you are mentally tired and cannot figure a way out, take a break. Otherwise, you will even mess up with the parts that were actually correct. This will make you extremely stressed out, and you will most likely <i>NOT</i> finish your assignment.<br>
 
Third, do not look up solutions on internet. If you are in first year and barely know few things, you will never understand solutions on internet. There are tons of people who write programs using different techniques than our university teaches. You will simply waste your time on looking for solutions. <br>
 
Third, do not look up solutions on internet. If you are in first year and barely know few things, you will never understand solutions on internet. There are tons of people who write programs using different techniques than our university teaches. You will simply waste your time on looking for solutions. <br>
Fourth, start assignments ahead of time. I used to start assignments a day before, and I had many troubles finishing them properly. Even if the first year computer science assignments are easy, they still take up some time. Besides, if you start taking second year computer science courses, you will NEVER finish an programming assignments in one day.  
+
Fourth, start assignments ahead of time. I used to start assignments a day before, and I had many troubles finishing them properly. Even if the first year computer science assignments are easy, they still take up some time. Besides, if you start taking second year computer science courses, you will NEVER finish an programming assignments in one day.<br>
 
These tips always helped me doing assignments. However, these are very general tips for doing assignments. As you experience more and more in programming, you will find out more tips that are more specific to your case.
 
These tips always helped me doing assignments. However, these are very general tips for doing assignments. As you experience more and more in programming, you will find out more tips that are more specific to your case.
  

Revision as of 19:08, 10 April 2010

Back to the Program-A-Day homepage

Problem

This question will help you practice with string methods and for loops.

Complete the program titled Palindrome that checks if a word is a palindrome.
If the word is palindrome, print out "It's a palindrome." using System.out.println().
Otherwise, print out the reversed order of the word.

For example:

  • "deed" and "civic" are palindromes.
  • "tomato" and "mama" are not palindromes.


Find out whether the two strings provided are palindromes or not.
The program that you need to complete is provided below.

public class Palindrome 
{
	public static void main(String[] args)
	{
		String testWord1 = "Arewenotdrawnonward,wefew,drawnonwardtonewera"
		String testWord2 = "Murderforajarofredrum"
			
		// Your code goes here
	}
}
 

By students..

Before taking Comp 1010 course, I had never had any programming experiences. I did not know much, and I have had many problems with assignments. I'd like to give you some tips that helped me with doing programming assignments.

First, always write comments. This may depend on each student's skill, however, you won't finish your assignments or writing codes in few hours. Then, when you work on it later you are very likely to forget what you were doing. Writing comments will help you remember what you were doing, and it will be easier for you to fix errors by going through each of the code.
Second, take a break if you are stuck. If you are mentally tired and cannot figure a way out, take a break. Otherwise, you will even mess up with the parts that were actually correct. This will make you extremely stressed out, and you will most likely NOT finish your assignment.
Third, do not look up solutions on internet. If you are in first year and barely know few things, you will never understand solutions on internet. There are tons of people who write programs using different techniques than our university teaches. You will simply waste your time on looking for solutions.
Fourth, start assignments ahead of time. I used to start assignments a day before, and I had many troubles finishing them properly. Even if the first year computer science assignments are easy, they still take up some time. Besides, if you start taking second year computer science courses, you will NEVER finish an programming assignments in one day.
These tips always helped me doing assignments. However, these are very general tips for doing assignments. As you experience more and more in programming, you will find out more tips that are more specific to your case.

Solution

First of all, you will need to declare two string objects to store the reversed words.
You will need to initialize the strings, which means make the strings empty.

String reverse1 = ""
String reverse2 = ""


Then, you will need a for loop to reverse the first word.
In order to loop through each character, you will need to set the for loop to run for the length of the string.

Getting a length of a string can be done by using

str.length();


The loop will start from 0 and finish looping when i >= testWord1.length().
The increment will be 1 since we are accessing each character from the string.

for(int i = 0; i < testWord1.length(); i++)
{
}


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.
charAt(i) is a string method that returns the ith character of a string.
The peusdo-code below concatenates each letter of the string in reversed order.

reverse1 = get the letters of the str by charAt(i) + reverse1


Then, you will need another for loop to reverse testWord2.
This time, we will use another approach and make the for loop start from the end.
It's strongly recommended to try this way, as it will give you a better understanding of how the for loop works.

for(int i = testWord2.length()-1; i >= 0; i--)
{
}


The code above starts from the length of testWord2 - 1, and ends when i becomes less than 0.
Why does it have to start from the string length - 1?
It's because there is no character at the length of the string. See the example below.
What is the length of the string, str = "apple"?
It's 5.
What is the index of the last character e in the string?
It's 4!

If you try to get a character at str.charAt(5), you will get a run-time error.
This is very important to know and you will be facing many variations of these errors later.

Now, back to the question.
We have to decrement it by 1 now, because we are reading from the last character of the string.
How would concatenation work now?

reverse2 = reverse2 + letters of the str by charAt(i)

str = "abcde"

reverse = reverse + charAt(i) of str from end would be
e
ed
edc
edcb
edcba

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
}


One thing to note: strings cannot be compared using the double equals operator '=='.
You will need a special method called:

testWord.equals(reversedTestWord).


Note: If you want to ignore capitalization for matching two strings, use equalsIgnoreCase() method.

str.equalsIgnoreCase()


See below for the solution code.

Code

Solution Code

Back to the Program-A-Day homepage