Alphabet Run Encryption - For this challenge you will be decoding a message.
Using the Java language, create the function AlphabetRunEncryption(str) read the str parameter being passed which will be an encrypted string and your program should output the original decrypted string. The encryption being used is the following: For every character i in str up to the second to last character, take the i and i+1 characters and encode them by writing the letters of the alphabet, in order, that range in the same direction between those chosen characters.
For example: if the original string were bo then it would be encoded as cdefghijklmn, but if the string were boa then bo is encoded as cdefghijklmn and oa is encoded as nmlkjihgfedcb with the final encrypted string being cdefghijklmnnmlkjihgfedcb. So str may be something like the encrypted string just written, and your program should decrypt it and output the original message. The input string will only contain lowercase characters (a...z). There are also three important rules to this encryption based on specific character sequences. 1) If the original string contains only one letter between two chosen characters, such as the string ac then this would be encrypted as bR with R standing for what direction in the alphabet to go in determining the original characters. The encrypted string bR represents ac but the encrypted string bL represents ca (R = right, L = left). 2) If the original string contains zero letters between two chosen characters, such as the string ab then this would be encrypted as abS, with S representing the fact that no decryption is needed on the two letters preceding S. For example, if the original string were aba then the encryption would be abSbaS, but be careful because decrypting this you get abba, but the actual original string is aba. 3) If the original string contains a repeat of letters, such as the string acc then this would be encrypted as bRcN, with N representing the fact that no change in characters occurred on the character preceding N. The input string will never only be composed of repeated characters.
Correct Sample Output
Input = "bcdefghijklmnopqrstN"Output = "att"
Input = "abSbaSaNbR"Output = "abaac"
Here is my code....
Expand|Select|Wrap|Line Numbers
- /*
- * Create a Decoder.
- *
- *
- */
- package finalproject;
- public class FinalProject {
- public static void main(String[] args) {
- // Creating the test strings
- //final should be "att"
- String testi = new String("bcdefghijklmnopqrstN");
- //final should be "abaac"
- String testii = new String("abSbaSaNbR");
- //not sure if this will be relevant
- char[] arrayi = testi.toCharArray();
- int[] asciI = new int[arrayi.length];
- char[] arrayii = testii.toCharArray();
- int[] asciIi= new int[arrayii.length];
- String translated = new String(); //using translated += temporary, adds letters one at a time.
- for (int i = 0; i < arrayi.length; i++){
- char temporary = 'A'; //declares a char
- // was used to test
- //System.out.print(arrayi[i]);
- char temp = arrayi[i];
- asciI[i] = ASCIItranslate(temp);
- if(i == 0){ //if its the first letter instantly go back a letter, to start off the decoder
- if (asciI[i]!= 'a'){
- temporary = (char)(asciI[i] - 1);
- translated += temporary;
- }
- else{
- temporary = (char)(asciI[i]);
- translated += temporary;
- }
- }
- else{ // if its not the first letter it checks for the rules.
- // 'S'
- if (asciI[i] == 'S' ){
- //do this
- translated += temporary;
- }// 'N'
- if(asciI[i] == 'N' ){
- removeLastChar(translated);
- temporary = (char)(asciI[i-1]);
- translated += temporary;
- translated += temporary;
- }
- if(asciI[i]== 'L'){
- temporary = (char)((asciI[i-1])-1);
- translated += temporary;
- }
- if (asciI[i]== 'R'){
- temporary = (char)((asciI[i-1])+1);
- translated += temporary;
- }
- }
- }
- System.out.println(translated);
- }
- //translates to ASCII which uses integers
- public static int ASCIItranslate(char letter) {
- return letter;
- }
- //removes the last char
- private static String removeLastChar(String str) {
- return str.substring(0,str.length()-1);
- }
- }
My problem is that when I run it with the selected
Expand|Select|Wrap|Line Numbers
- String testi = new String("bcdefghijklmnopqrstN");
Expand|Select|Wrap|Line Numbers
- String testii = new String("abSbaSaNbR");
instead of the output being abaac i get abaaac. Im about to rack my brain on this one. I know that if I change my code of
Expand|Select|Wrap|Line Numbers
- if(asciI[i] == 'N' ){
- removeLastChar(translated);
- temporary = (char)(asciI[i-1]);
- translated += temporary;
- translated += temporary;
Expand|Select|Wrap|Line Numbers
- if(asciI[i] == 'N' ){
- removeLastChar(translated);
- temporary = (char)(asciI[i-1]);
- translated += temporary;
and remove the second translation and run it by switching the test from test i to test ii, I get the correct output for the "abaac" but then I lose a t on the "att" and it only displays as "at". What am I missing or what am I doing wrong?!?! Any help is greatly appreciated!!!