Difference between revisions of "Secret Code"

From CompSciWiki
Jump to: navigation, search
 
 
(34 intermediate revisions by 7 users not shown)
Line 1: Line 1:
The following string of text has a secret code encrypted inside it. In order to decipher it, you will need to:
+
{{1010PrAD
1) Extract every fourth letter of the string
+
|ProblemName=Secret Code
2) Reverse the order of the extracted letters
+
|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:
3) Set the case of the extracted letters so that they alternate between uppercase and lowercase,
+
  with the first letter being uppercase.
+
 
+
The following is the string that must be deciphered:
+
  
xmydartlkarrsgeoptwwjvbodeblxgbleivetoihewt
+
xmyaartikarosgelptwtjvbldebixgbieivbtoieewt
  
To solve this problem:
+
In order to decipher the password, you will need to:
You will need three string variables:
+
- one called "message" to store the above string to be deciphered
+
- one called "result1" to store the intermediate result
+
- one called "secretCode" to reverse the order of the letters in "result1".
+
  
To extract every fourth letter, you will need the length method, charAt() method, a while loop, a counter, and the "message" and "result1" variables.
+
1) Extract every fourth letter of the string<br>
 +
2) Reverse the order of the extracted letters<br>
 +
3) Shift each letter to the letter three letters ahead in the alphabet<br>
 +
4) Set the cases of the extracted letters so that they alternate between uppercase and lowercase, with the first letter being uppercase.
  
To reverse the order of the letters, you will need the length() and charAt() methods, a while loop, and the "result1" and "secretCode" variables.
+
The solution will require the following components:<br>
 +
1) Three string variables:<br>
 +
- one called "message" to store the string being deciphered<br>
 +
- one called "s" to store the resulting string after extracting every fourth letter<br>
 +
- one called "password" to store the results after Steps 2, 3, and 4<br>
 +
2) The length(), charAt(), and toUpperCase() methods<br>
 +
3) A counter variable<br>
 +
4) String concatenation<br>
 +
5) The mod operator (&#37;)
  
Note that because the first char in a string is index 0, the last char will actually be
+
|SideSectionTitle=String Methods and Debugging
length - 1. For example, for a string with 5 chars, the indexes will be from 0 to 4.
+
|SideSection=
 +
[[Image:Wiki_array02.jpg|center]]<BR>
  
To alternate the cases, you will need the length(), charAt(), toLowerCase(), and toUpperCase() methods, a while loop, and the "secretCode" variable.
+
|Solution=
 +
You may want to use JOptionPane.showMessageDialog() to verify that the contents of the string variables are correct after each step.<br><br>
 +
The first step is to declare and initialize the necessary variables. Counter is initialized to 3, which is the index of the fourth letter. This is because we start numbering the string positions at 0, not 1.
 +
{{CodeBlock
 +
|Code=
 +
//declare variables
 +
String message = "xmyaartikarosgelptwtjvbldebixgbieivbtoieewt";
 +
String s = "";
 +
String password = "";
 +
String convertedPassword = "";
 +
char currentCharacter;
 +
int counter = 3;
 +
}}
  
 +
The second step is to extract every fourth letter and append it to 's' using the string concatenation operation. We then increment the counter by 4 to advance to the next letter that we need. This is repeated until the end of the message is reached. The string s should contain "aioltliibe" after this step.
 +
{{CodeBlock
 +
|Code=
 +
//extract every fourth letter
 +
while(counter < message.length())
 +
{
 +
  s += message.charAt(counter);
 +
  counter += 4;
 +
}
 +
}}
  
Solution
+
The counter now needs to be set to the last char in the string, for the reversal stage.
 +
{{CodeBlock
 +
|Code=
 +
//reset counter to end of s
 +
counter = s.length() - 1;
 +
}}
  
public class DecodeMessage
+
The next step is to move through the chars in 's', in reverse order, and store the chars in forward order in 'password'. The string password should contain "ebiiltloia" after this step.
 +
{{CodeBlock
 +
|Code=
 +
//traverse s from the end to the beginning and store the letters in password
 +
while(counter >= 0)
 
{
 
{
public static void main(String[] args)
+
  password += s.charAt(counter);
{
+
  counter--;
String message;
+
}
String result1 = "";
+
}}
String secretCode= "";
+
int counter = 4;
+
+
message = "xmydartlkarrsgeoptwwjvbodeblxgbleivetoihewt";
+
+
//extract every fourth letter
+
while(counter < message.length)
+
{
+
result1 += message.charAt(counter);
+
counter += 4;
+
}
+
  
+
The final section combines the last two steps: shifting each letter ahead in the alphabet by three letters, and alternating the cases between upper and lower case. The counter will already be set to 0 from the previous step, which is what we need. If the counter is even, we set the letter to uppercase, otherwise we leave it in lowercase.
//reset counter to end of result1
+
{{CodeBlock
counter = result1.length - 1;
+
|Code=
+
//change each letter to the one three letters ahead in the alphabet
//traverse result1 from the end to the beginning and store the letters in password
+
//alternate the cases in password. Counter is now at 0 so it doesn't need to be reset
while(counter >= 0)
+
while(counter < password.length())
 +
{
 +
  if(counter % 2 == 0)
 +
  {
 +
    currentCharacter = (char)(password.toUpperCase().charAt(counter) + 3);
 +
  } else {
 +
    currentCharacter = (char)(password.charAt(counter) + 3);
 +
  }
 +
 
 +
  convertedPassword += currentCharacter;
 +
  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 (i.e. the password) should be "HeLlOwOrLd".
 +
{{CodeBlock
 +
|Code=
 +
//display the decoded password
 +
JOptionPane.showMessageDialog("The password is: " + password);
 +
}}
 +
|SolutionCode = import javax.swing.*;
 +
 
 +
public class decodePassword
 +
{
 +
    public static void main(String[] args)
 +
    {
 +
        //declare variables
 +
        String message = "xmyaartikarosgelptwtjvbldebixgbieivbtoieewt";
 +
        String s = "";
 +
        String password = "";
 +
String convertedPassword = "";
 +
char currentCharacter;
 +
        int counter = 3;
 +
 
 +
        //extract every fourth letter
 +
        while(counter < message.length())
 +
        {
 +
            s += message.charAt(counter);
 +
            counter += 4;
 +
        }
 +
 
 +
        //reset counter to end of s
 +
        counter = s.length() - 1;
 +
 
 +
        //traverse s from the end to the beginning and store the letters in password
 +
        while(counter >= 0)
 +
        {
 +
            password += s.charAt(counter);
 +
            counter--;
 +
        }
 +
 
 +
counter = 0;
 +
 
 +
        //change each letter to the one three letters ahead in the alphabet
 +
        //alternate the cases in password. Counter is now at 0 so it doesn't need to be reset
 +
        while(counter < password.length())
 +
        {
 +
if(counter % 2 == 0)
 
{
 
{
secretCode+= result1.charAt(counter);
+
currentCharacter = (char)(password.toUpperCase().charAt(counter) + 3);
counter--;
+
} else {
 +
currentCharacter = (char)(password.charAt(counter) + 3);
 
}
 
}
 
//alternate the cases in result 2. Counter is now at 0 so it doesn't need to be reset
 
while(counter < password.length)
 
{
 
if(counter%2 == 0)
 
{
 
secretCode.charAt(counter).toUpperCase();
 
}
 
counter++;
 
}
 
}
 
}
 
  
The final output (ie. the secret code) should be HeLlOwOrLd
+
convertedPassword += currentCharacter;
 +
counter++;
 +
        }
 +
 
 +
        //display the decoded password
 +
        JOptionPane.showMessageDialog(null, "The password is: " + convertedPassword);
 +
    }
 +
}
 +
}}

Latest revision as of 19:48, 25 June 2012

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 the password, 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 cases of the extracted letters so that they alternate between uppercase and lowercase, with the first letter being uppercase.

The solution will require the following components:
1) Three string variables:
- one called "message" to store the string being deciphered
- one called "s" to store the resulting string after extracting every fourth letter
- one called "password" to store the results after Steps 2, 3, and 4
2) The length(), charAt(), and toUpperCase() methods
3) A counter variable
4) String concatenation
5) The mod operator (%)

 

String Methods and Debugging

Wiki array02.jpg

Solution

You may want to use JOptionPane.showMessageDialog() to verify that the contents of the string variables are correct after each step.

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

 //declare variables
String message = "xmyaartikarosgelptwtjvbldebixgbieivbtoieewt";
String s = "";
String password = "";
String convertedPassword = "";
char currentCharacter;
int counter = 3; 

The second step is to extract every fourth letter and append it to 's' using the string concatenation operation. We then increment the counter by 4 to advance to the next letter that we need. This is repeated until the end of the message is reached. The string s should contain "aioltliibe" after this step.

 //extract every fourth letter
while(counter < message.length())
{
  s += message.charAt(counter);
  counter += 4;
} 

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

 //reset counter to end of s
counter = s.length() - 1; 

The next step is to move through the chars in 's', in reverse order, and store the chars in forward order in 'password'. The string password should contain "ebiiltloia" after this step.

 //traverse s from the end to the beginning and store the letters in password
while(counter >= 0)
{
  password += s.charAt(counter);
  counter--;
} 

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

 //change each letter to the one three letters ahead in the alphabet
//alternate the cases in password. Counter is now at 0 so it doesn't need to be reset
while(counter < password.length())
{
  if(counter % 2 == 0)
  {
    currentCharacter = (char)(password.toUpperCase().charAt(counter) + 3);
  } else {
    currentCharacter = (char)(password.charAt(counter) + 3);
  }

  convertedPassword += currentCharacter;
  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 (i.e. the password) should be "HeLlOwOrLd".

 //display the decoded password
JOptionPane.showMessageDialog("The password is: " + password); 

Code

Solution Code

Back to the Program-A-Day homepage