Difference between revisions of "Secret Code"

From CompSciWiki
Jump to: navigation, search
Line 18: Line 18:
 
- one called "password" to store the results after Steps 2, 3, and 4
 
- one called "password" to store the results after Steps 2, 3, and 4
  
2) The length(), charAt(), toUpper(), and toLower() methods<br>
+
2) The length(), charAt(), and toUpperCase() methods<br>
 
3) String concatenation<br>
 
3) String concatenation<br>
4) The mod operator  
+
4) The mod operator (&#37;)
  
 
|SideSection=
 
|SideSection=

Revision as of 23:54, 5 April 2010

Back to the Program-A-Day homepage

Problem

You have just been given access to a top secret database, however the password has been sent to you by encrypting it in the following string of text:

xmyaartikarosgelptwtjvbldebixgbieivbtoieewt

In order to decipher it, you will need to:

1) Extract every fourth letter of the string
2) Reverse the order of the extracted letters
3) Shift each letter to the letter three letters ahead in the alphabet
4) Set the case of the extracted letters so that they alternate between uppercase and lowercase, with the first letter being uppercase.

The solution will require the following elements:
1) Three string variables:
- one called "message" to store the above string to be deciphered
- one called "s" to store the resulting string after reversing the extracted letters
- one called "password" to store the results after Steps 2, 3, and 4

2) The length(), charAt(), and toUpperCase() methods
3) String concatenation
4) The mod operator (%)

 

SideSectionTitle

float
Taken from http://www.flickr.com/photos/daniello/565304023/

An image or By Students section

Solution

1. The first step is to declare and initialize the necessary variables. Counter is initialized to 3, because we start numbering the string indices at 0, not 1. Therefore the fourth letter is actually at index 3.

String message = "xmyaartikarosgelptwtjvbldebixgbieivbtoieewt";
String s = "";
String password = "";
int counter = 3;

2. The second step is to extract every fourth letter and append it to s using the string concatenation operation, then increment the counter by 4. This is repeated until the end of the message is reached.

while(counter < message.length)
{
    s += message.charAt(counter);
    counter += 4;
}

3. The counter now needs to be set to the last char in the string, for the reversal stage.

counter = s.length - 1;

4. The next step is to move through the chars in s, in reverse order, and store the result in "password".

while(counter >= 0)
{
    password += message.charAt(counter);
    counter--;
}

5. The final section combines the last two steps: moving each letter ahead by three letters, and alternating the cases between upper and lower case. Counter will already be set to 0 from the previous step, which is what we need. If the counter is even, we set it to uppercase, otherwise we leave it in lowercase.

while(counter < password.length)
{
    password(counter) += 3;

    if(counter % 2 == 0)
    {
	password.charAt(counter).toUpperCase();
    }
    counter++;
}

6. Now that we have deciphered the password, display it in a message dialog so we can see what it is. The final output (ie. the password) should be "MaDhAtTeR".

JOptionPane.showMessageDialog("The password is: " + password);

Code

Solution Code

Back to the Program-A-Day homepage