Difference between revisions of "Secret Code"
Line 1: | Line 1: | ||
− | + | {{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: | ||
− | + | xmydartlkarrsgeoptwwjvbodeblxgbleivetoihewt | |
− | + | In order to decipher it, you will need to: | |
− | + | 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. | ||
− | + | 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 | ||
− | + | |SideSection= | |
+ | [[Image:OperatingSystemExample.jpg|float|267px]] | ||
+ | <BR> | ||
+ | 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. | ||
+ | <pre> | ||
+ | String message = "artrkaresgetptwtjvbadebhxgbdeivatoimewt"; | ||
+ | String s = ""; | ||
+ | String password = ""; | ||
+ | int counter = 3; | ||
+ | </pre> | ||
− | + | 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 | + | 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> | ||
− | + | 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. | |
− | + | <pre> | |
− | + | while(counter < password.length) | |
− | + | { | |
+ | if(counter % 2 == 0) | ||
+ | { | ||
+ | password.charAt(counter).toUpperCase(); | ||
+ | } | ||
+ | counter++; | ||
+ | } | ||
+ | </pre> | ||
− | + | 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.*; | ||
− | + | public class decodePassword | |
+ | { | ||
+ | public static void main(String[] args) | ||
+ | { | ||
+ | String message = "xmydartlkarrsgeoptwwjvbodeblxgbleivetoihewt"; | ||
+ | String s = ""; | ||
+ | String password = ""; | ||
+ | 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--; | ||
+ | } | ||
− | + | //alternate the cases in s. Counter is now at 0 so it doesn't need to be reset | |
− | + | while(counter < password.length) | |
− | + | { | |
− | + | if(counter % 2 == 0) | |
− | + | { | |
+ | 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
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: xmydartlkarrsgeoptwwjvbodeblxgbleivetoihewt In order to decipher it, you will need to: 1)Extract every fourth letter of the string The solution will require the following elements: 2) The length(), charAt(), toUpper(), and toLower() methods |
SideSectionTitle
An image or By Students section | |
---|---|---|
Solution1. 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 |