Difference between revisions of "Secret Code"
m (Removed <pre> tags from SolutionCode.) |
|||
Line 28: | Line 28: | ||
|Solution= | |Solution= | ||
You may want to use JOptionPane.showMessageDialog() to verify that the contents of the string variables are correct after each step.<br><br> | 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. | |
<pre> | <pre> | ||
//declare variables | //declare variables | ||
Line 37: | Line 37: | ||
</pre> | </pre> | ||
− | + | 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. | |
<pre> | <pre> | ||
//extract every fourth letter | //extract every fourth letter | ||
Line 47: | Line 47: | ||
</pre> | </pre> | ||
− | + | The counter now needs to be set to the last char in the string, for the reversal stage. | |
<pre> | <pre> | ||
//reset counter to end of s | //reset counter to end of s | ||
Line 53: | Line 53: | ||
</pre> | </pre> | ||
− | + | 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. | |
<pre> | <pre> | ||
//traverse s from the end to the beginning and store the letters in password | //traverse s from the end to the beginning and store the letters in password | ||
Line 63: | Line 63: | ||
</pre> | </pre> | ||
− | + | 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. | |
<pre> | <pre> | ||
//change each letter to the one three letters ahead in the alphabet | //change each letter to the one three letters ahead in the alphabet |
Revision as of 19:10, 9 April 2010
Back to the Program-A-Day homepage
ProblemYou 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 The solution will require the following components: |
String Methods and Debugging | |
---|---|---|
SolutionYou may want to use JOptionPane.showMessageDialog() to verify that the contents of the string variables are correct after each step. //declare variables String message = "xmyaartikarosgelptwtjvbldebixgbieivbtoieewt"; String s = ""; String password = ""; 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) { 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 (i.e. the password) should be "HeLlOwOrLd". //display the decoded password JOptionPane.showMessageDialog("The password is: " + password); | ||
Code |