/**
* Encrypts words by shift letters
*
* Sean Appaneal
*
* v1.0
*/
import java.util.*;
public class Caesar
{
public static void main(String[] args) {
boolean keepGoing = true;
while(keepGoing){
//getting word, and number to be shifted
Scanner sc = new Scanner(System.in);
System.out.println("Please input the word you would like to encrypt");
String og = sc.nextLine();
int shift;
String enc = "";
System.out.println("Please input the number of letter you would like to shift");
shift = sc.nextInt();
String alph = "abcdefghijklmnopqrstuvwxyz ";
int ln = og.length();//finding length
for (int i=0; i < ln; i++){//for loop to run through word
String temp = og.substring(i, i + 1);//change word, loop around if a 'z'
int point = alph.indexOf(temp);
int ex = (point + shift) % 26;
String r = alph.substring(ex, ex + 1);
System.out.print(r);
}
System.out.println(" ");//blank line cause otherwise shifted prints on same line as restart
System.out.println("Press 1 to continue, press 2 to stop");//restart program
int go = sc.nextInt();
if(go != 1)
keepGoing = false;
}//end of while
}//end of main
}//end of class
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.