Reputation: 652
I've been given an assignment for my CompSci class in which we are required to create a program that uses a Caesar Cipher to encrypt a string of text, however I've hit a wall now that my code is doing something funny.
I started off by first creating an array of strings containing each letter of the alphabet. I then take the message entered by the user, split it into words, then into letters. Each of those letters then gets put into an array called letters
.Once I have my array of letters, I call the 'getLetterIndex` function.
Within the getLetterIndex
function, I have an outer for-loop counting from i = 0
to i = 25
(25 being the last index in the alphabet array) and an inner for-loop iterating though each letter in the array letters
. If alphabet[i]
is equal to a letter within the letters
array, the value of i
is added to an array called letterIndexes
. Thus each letter is converted to a numerical representation of it's placement within the alphabet.
I've been able to get each letter converted to an index value but my array of these letter indexes is somehow getting sorted from smallest to largest.
static String[] alphabet = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
public static void tokenizeEnteredMessage(String message) {
String[] words = message.split(" ");
String[] letters = null;
for (String word : words) {
letters = word.toUpperCase().split("");
}
getLetterIndex(letters);
}
public static void getLetterIndex(String[] letters) {
int indexCount = 0;
int[] letterIndexes = new int[letters.length];
for (int i = 0; i < alphabet.length; i++) {
for (String letter : letters) {
if (alphabet[i].equals(letter)) {
letterIndexes[indexCount] = i;
indexCount++;
}
}
}
Output with message "apple" and a cipher shift of 3:
Enter '0' to type a phrase or enter '1' to specify the path to a text document: 0
Enter message that you would like to have encrypted. Do not enter numerical values: apple
Enter the key you would like to use to encrypt (integer): 3
A P P L E
Original Position: 0 4 11 15 15
Position After Shift: 3 7 14 18 18
Encrypted Message:
D H O S S
You can see above that each letter did correctly get converted to it's position in the alphabet and the position shift was completed successfully, but the indexes are out of order for some reason. The "Original Position" should be: 0 15 15 11 4.
Oddly enough it works perfectly fine when I enter the alphabet as my message to be encrypted:
Enter '0' to type a phrase or enter '1' to specify the path to a text document: 0
Enter message that you would like to have encrypted. Do not enter numerical values: abcdefghijklmnopqrstuvwxyz
Enter the key you would like to use to encrypt (integer): 3
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Original Position: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Position After Shift: 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 0 1 2
Encrypted Message:
D E F G H I J K L M N O P Q R S T U V W X Y Z A B C
I should also mention that my professor has asked us not to use any substring methods, but to stick to only using arrays as part of the assignment's requirements. I'm sure theres a simple mistake I've overlooked, but I cant seem to catch it. I'm fairly new to Java and have a tendency to overcomplicate simple problems, so any help with this is much appreciated!
Upvotes: 0
Views: 4404
Reputation: 995
You will probably want to look into String's toCharArray()
(String's Java Doc) as well as how a char
is used with ASCII values. By converting each string in your list to an array of characters, you can manipulate their values numerically.
Throw the following into a tester method:
char A = 'A';
A++;
System.out.println(A);
That will print out the letter 'B'. Now think about what should happen if you have the letter 'Z' and it's incremented. You would want to go back to 'A'.
char Z = 'Z';
if(Z < 'Z') {
Z++; // If we haven't reached 'Z' (and are only shifting by 1), increment away!
} else {
Z = 'A'; // Otherwise, reset it to 'A'
}
This logic only works when shifting the characters by 1. You'll want a generic approach for ALL possible values X
(assuming X
is greater than 0). That's where the modulo %
operator comes in handy! You know X
only has 26 possible values, so if the user entered a larger number like 50, you just need to know 50%26 = 24
, which gives you your shifting value X
.
Now that you have all of this logic, keep in mind that arrays/lists maintain their order, so the original ordering of the letters in each word within your list of strings will remain the same.
Upvotes: 0