/**
* Write a short description of what the class does (one line)
* @author (your name)
* @version (a version number or a date)
*/
import java.util.*;
public class CaesarShift{ //class header
public static void main(String[] args) {
String alphabet = "abcdefghijklmnopqrstuvwxyz";
String letterOfWord = "";
Scanner sc = new Scanner(System.in);
boolean keepCoding = true;
while(keepCoding){ //asks again for a word and shift amount
boolean keepPlugging = true;
int counter = 0;
System.out.println("Print in word: ");
String word = sc.next();
int lengthWord = word.length();
System.out.println("How much shifted? ");
int shift = sc.nextInt();
while (keepPlugging){ //keeps plugging in letters of the word
letterOfWord = word.substring(counter, counter + 1);
int letter = alphabet.indexOf(letterOfWord); //letter of the word found on the alphabet
letter = letter + shift;
letter = letter % 26; //to loop around the alphabet
letterOfWord = alphabet.substring(letter, letter + 1);
System.out.print(letterOfWord); //print to connect the letters
counter++;
if(counter > lengthWord - 1){
keepPlugging = false;
}
}
System.out.println("");
System.out.println("Code another word?");
String codeAnother = sc.next();
if(codeAnother.equals("no")){
keepCoding = false;
}
}
}
}
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.