Difference between revisions of "Secret Code"

From CompSciWiki
Jump to: navigation, search
Line 1: Line 1:
== Secret Code ==
+
{{1010PrAD
 +
|ProblemName=Secret Code
 +
|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:
  
{{1010PrAD|ProblemName=Secret Code
+
xmydartlkarrsgeoptwwjvbodeblxgbleivetoihewt
  
|Problem= Secret Code
+
In order to decipher it, you will need to:
  
The following string of text has a secret code encrypted inside it:
+
1)Extract every fourth letter of the string<br>
 +
2)Reverse the order of the extracted letters<br>
 +
3)Set the case of the extracted letters so that they alternate between uppercase and lowercase, with the first letter being uppercase.
  
xmydartlkarrsgeoptwwjvbodeblxgbleivetoihewt
+
The solution will require the following elements:<br>
 +
1) Three string variables:<br>
 +
- one called "message" to store the above string to be deciphered<br>
 +
- one called "s" to store the result after Step 1<br>
 +
- one called "password" to store the results after Steps 2 and 3
  
 +
2) The length(), charAt(), toUpper(), and toLower() methods<br>
 +
3)String concatenation<br>
 +
4)The mod operator
  
In order to decipher it, you will need to:
+
|SideSection=
 +
[[Image:OperatingSystemExample.jpg|float|267px]]
 +
<BR>
 +
Taken from http://www.flickr.com/photos/daniello/565304023/
  
1) Extract every fourth letter of the string
+
An image or By Students section
  
2) Reverse the order of the extracted letters
+
|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.
 +
<pre>
 +
String message = "artrkaresgetptwtjvbadebhxgbdeivatoimewt";
 +
String s = "";
 +
String password = "";
 +
int counter = 3;
 +
</pre>
  
3) Set the case of the extracted letters so that they alternate between uppercase and lowercase, with the first letter being uppercase.
+
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.
 +
<pre>
 +
while(counter < message.length)
 +
{
 +
    s += message.charAt(counter);
 +
    counter += 4;
 +
}
 +
</pre>
  
 +
3. The counter now needs to be set to the last char in the string, for the reversal stage.
 +
<pre>
 +
counter = s.length - 1;
 +
</pre>
  
The solution will require the following elements:
+
4. The next step is to move through the chars in s, in reverse order, and store the result in "password".
 +
<pre>
 +
while(counter >= 0)
 +
{
 +
    password += message.charAt(counter);
 +
    counter--;
 +
}
 +
</pre>
  
1) You will need three string variables:
+
5. The final step is to alternate the cases in password 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.
- one called "message" to store the above string to be deciphered
+
<pre>
- one called "result1" to store the result after Step 1
+
while(counter < password.length)
- one called "secretCode" to store the results after Steps 2 and 3
+
{
 +
    if(counter &#37; 2 == 0)
 +
    {
 +
password.charAt(counter).toUpperCase();
 +
    }
 +
    counter++;
 +
}
 +
</pre>
  
2) The length(), charAt(), toUpper(), and toLower() methods
+
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".
 +
<pre>
 +
JOptionPane.showMessageDialog("The password is: " + password);
 +
</pre>
 +
|SolutionCode =
 +
<pre>
 +
import javax.swing.*;
  
3) String concatenation
+
public class decodePassword
 +
{
 +
    public static void main(String[] args)
 +
    {
 +
        String message = "xmydartlkarrsgeoptwwjvbodeblxgbleivetoihewt";
 +
        String s = "";
 +
        String password = "";
 +
        int counter = 3;
  
4) The mod operator
+
        //extract every fourth letter
 +
        while(counter < message.length)
 +
        {
 +
            s += message.charAt(counter);
 +
            counter += 4;
 +
        }
  
 +
        //reset counter to end of s
 +
        counter = s.length - 1;
  
|SideSection=
+
        //traverse s from the end to the beginning and store the letters in password
[[Image:OperatingSystemExample.jpg|float|267px]]
+
        while(counter >= 0)
<BR>
+
        {
Taken from http://www.flickr.com/photos/daniello/565304023/
+
            password += s.charAt(counter);
 +
            counter--;
 +
        }
  
An image or By Students section
+
        //alternate the cases in s. Counter is now at 0 so it doesn't need to be reset
 
+
        while(counter < password.length)
|Solution=The solution...
+
        {
[SolutionCode =
+
            if(counter &#37; 2 == 0)
<pre>
+
            {
 +
                password.chatAt(counter).toUpperCase();
 +
            }
 +
            counter++;
 +
        }
  
 +
        //display the decoded password
 +
        JOptionPane.showMessageDialog("The password is: " + password);
 +
    }
 +
}
 
</pre>
 
</pre>
 
}}
 
}}

Revision as of 00:32, 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:

xmydartlkarrsgeoptwwjvbodeblxgbleivetoihewt

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)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 result after Step 1
- one called "password" to store the results after Steps 2 and 3

2) The length(), charAt(), toUpper(), and toLower() 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 = "artrkaresgetptwtjvbadebhxgbdeivatoimewt";
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 step is to alternate the cases in password 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)
{
    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