Skip to content

Instantly share code, notes, and snippets.

@memish
Created July 12, 2018 00:37
Show Gist options
  • Select an option

  • Save memish/0084ed3b23dc814140fc8044491f2f0b to your computer and use it in GitHub Desktop.

Select an option

Save memish/0084ed3b23dc814140fc8044491f2f0b to your computer and use it in GitHub Desktop.
import java.util.*;
public class Cryptogram
{
public Cryptogram()
{
}
public String Encrypt(String start){
char[] alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#*&%".toCharArray();
String[] al=new String[alphabet.length];//you can use this array for finding the location of character in the alphabet
for(int i=0; i < alphabet.length; i++){// this sets each letter of alphabet into string array al[]
al[i]=String.valueOf(alphabet[i]);
}
int modAmount = alphabet.length;
Scanner sc = new Scanner(System.in);
// System.out.println("Enter Text to be encrypted (letters only)");
String quote= start;//sc.nextLine();
char[] quoteArray = quote.toCharArray();
String[] QA =new String[quoteArray.length];//strings are easier to compare
for(int i=0; i < quoteArray.length; i++){// Sets each letter of the entered text to QA array
QA[i]=String.valueOf(quoteArray[i]);
}
//START HERE: You don't really need to look at code above
//keys to remember: the array al[] holds all 26 letters of alphabet
//the QA[] array holds each character of the text the user entered
int shift = 5;//use this as your shift
/* It's up to you if you want to use the for loops provided below.
you can ignore them or use them
* you can test that your encryption worked by using website below
* https://learncryptography.com/classical-encryption/caesar-cipher
*/
String encrypt = "";
for(int i=0; i < QA.length; i++){
for(int j=0; j < al.length; j++){
if(QA[i].equals(al[j])){//found the letter in al
int spot = (j + shift)%modAmount;
encrypt += al[spot];
}
}
}
return encrypt;
//lastly, display the encrypted characters to the screen
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment