# First Variation on Caesar Cipher in Java

## The problem#

The motion of a Caesar cipher is to interchange every plaintext letter (plaintext letters are from ‘a’ to ‘z’ or from ‘A’ to ‘Z’) with a distinct one a hard and fast variety of locations up or down the alphabet.

This program performs a variation of the Caesar shift. The shift will increase by 1 for every character (on every iteration).

If the shift is initially 1, the primary character of the message to be encoded might be shifted by 1, the second character might be shifted by 2, and many others…

### Coding: Parameters and return of perform “movingShift”#

`param s: a string to be coded`

`param shift: an integer giving the preliminary shift`

The perform “movingShift” first codes your complete string and then returns an array of strings containing the coded string in 5 elements (5 elements as a result of, to keep away from extra dangers, the coded message might be given to 5 runners, one piece for every runner).

If potential the message might be equally divided by message size between the 5 runners. If this isn’t potential, elements 1 to five could have subsequently non-increasing lengths, such that elements 1 to 4 are at the very least so long as when evenly divided, however at most 1 longer. If the final half is the empty string this empty string should be proven within the ensuing array.

For instance, if the coded message has a size of 17 the 5 elements could have lengths of 4, 4, 4, 4, 1. The elements 1, 2, 3, 4 are evenly cut up and the final a part of size 1 is shorter. If the size is 16 the elements might be of lengths 4, 4, 4, 4, 0. Components 1, 2, 3, 4 are evenly cut up and the fifth runner will keep at residence since his half is the empty string. If the size is 11, equal elements could be of size 2.2, therefore elements might be of lengths 3, 3, 3, 2, 0.

Additionally, you will implement a “demovingShift” perform with two parameters

### Decoding: parameters and return of perform “demovingShift”#

1. an array of strings: s (presumably ensuing from “movingShift”, with 5 strings)

2. an int shift

“demovingShift” returns a string.

### Instance:#

`u = "I ought to have identified that you'd have an ideal reply for me!!!"`

`movingShift(u, 1)` returns :

`v = ["J vltasl rlhr ", "zdfog odxr ypw", " atasl rlhr p ", "gwkzzyq zntyhv", " lvz wp!!!"]`

(quotes added with a view to see the strings and the areas, your program received’t write these quotes, see Instance Take a look at Instances)

and `demovingShift(v, 1)` returns u.

### Reference#

Caesar Cipher : http://en.wikipedia.org/wiki/Caesar_cipher

## The answer in Java code#

Choice 1:

``````import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.Listing;
import java.util.ArrayList;

public class CaesarCipher {
public static Listing<String>  movingShift(String s, int shift) {
int firstPartsSize;
if(s.size()%5==0) firstPartsSize=s.size()/5;
else firstPartsSize=s.size()/5+1;

char[] chars = s.toCharArray();
for(int i = 0; i<chars.size; i++){
chars[i] = Character.isLetter(chars[i])
?Character.isUpperCase(chars[i])
?(char)((((int)chars[i]-(int)'A')+shift+i)%26+(int)'A')
:(char)((((int)chars[i]-(int)'a')+shift+i)%26+(int)'a')
:chars[i];
}
String coded = new String(chars);
return new ArrayList<String>(){
{
}
};
}

public static String demovingShift(Listing<String> s, int shift) {
String coded = s.stream().accumulate(Collectors.becoming a member of());
char[] chars = coded.toCharArray();
for(int i = 0; i<chars.size; i++){
chars[i] = Character.isLetter(chars[i])
?Character.isUpperCase(chars[i])
?(char)((((int)chars[i]-(int)'A')-shift-i+(1+i/26)*26)%26+(int)'A')
:(char)((((int)chars[i]-(int)'a')-shift-i+(1+i/26)*26)%26+(int)'a')
:chars[i];
}
return new String(chars);
}

}
``````

Choice 2:

``````import java.util.ArrayList;
import java.util.Listing;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

public class CaesarCipher {
non-public static String upAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", downAlpha = "abcdefghijklmnopqrstuvwxyz";
public static Listing<String> movingShift(String s, int dist) {return cut up(shift(s, dist, 1));}
public static String demovingShift(Listing<String> s, int dist) {return shift(String.be part of("", s), -dist, -1);}
non-public static String shift(String s, int dist, int step) {
AtomicInteger rely = new AtomicInteger();
return s.chars().mapToObj(i -> (char)i).map(c -> {
boolean upC = upAlpha.comprises("" + c), downC = downAlpha.comprises("" + c);
if(!upC && !downC) {
rely.getAndIncrement();
return c.toString();
}
String lib = upC ? upAlpha : downAlpha;
int loc = (rely.getAndIncrement() * step + lib.indexOf(c) + dist) % 26;
loc = loc >= 0 ? loc : 26 + loc;
return "" + lib.charAt(loc);
}).accumulate(Collectors.becoming a member of());
}

non-public static Listing<String> cut up(String s) {
int sz = (int) Math.ceil(s.size() / 5.0);
Listing<String> out = new ArrayList<>();
return out;
}
}
``````

Choice 3:

``````import java.util.Listing;
import java.util.ArrayList;

public class CaesarCipher {

public static Listing<String>  movingShift(String s, int shift) {
s = convert(s, shift, 1);

Listing<String> ans = new ArrayList<String>();
int elements = (int) Math.ceil(s.size()/5.0);
for (int i = 0 ; i< 3*elements+1 ; i+=elements)

return ans;
}

public static String  demovingShift(Listing<String> s, int shift) {
return convert(String.be part of("",s), shift, -1);
}

non-public static String convert(String s, int shift, int sens) {
StringBuilder sb = new StringBuilder(s);
int delta = 0;
int mod4Neg = 26*(sb.size()/26 + 1);

for (int i = 0 ; i < sb.size() ; i++) {
if      (Character.isUpperCase(sb.charAt(i))) delta = 65;
else if (Character.isLowerCase(sb.charAt(i))) delta = 97;
else                                          proceed;
sb.setCharAt(i, (char) (((int) sb.charAt(i) - delta + mod4Neg + (shift+i) * sens) % 26 + delta) );
}
return sb.toString();
}
}
``````

## Take a look at circumstances to validate our answer#

``````import static org.junit.Assert.*;
import org.junit.Take a look at;
import java.util.*;

public class CaesarCipherTest {

@Take a look at
public void test1() {
String u = "I ought to have identified that you'd have an ideal reply for me!!!";
Listing<String> v = Arrays.asList("J vltasl rlhr ", "zdfog odxr ypw", " atasl rlhr p ", "gwkzzyq zntyhv", " lvz wp!!!");
assertEquals(v, CaesarCipher.movingShift(u, 1));
assertEquals(u, CaesarCipher.demovingShift(CaesarCipher.movingShift(u, 1), 1));
}
}
``````

## Further check circumstances#

``````import static org.junit.Assert.*;
import org.junit.Take a look at;
import java.util.*;

public class CaesarCipherTest {

@Take a look at
public void test1() {
String u = "I ought to have identified that you'd have an ideal reply for me!!!";
Listing<String> v = Arrays.asList("J vltasl rlhr ", "zdfog odxr ypw", " atasl rlhr p ", "gwkzzyq zntyhv", " lvz wp!!!");
assertEquals(u, CaesarCipher.demovingShift(CaesarCipher.movingShift(u, 1), 1));
assertEquals(v, CaesarCipher.movingShift(u, 1));
}
@Take a look at
public void test2() {
String u = "O CAPTAIN! my Captain! our fearful journey is finished;";
assertEquals(u, CaesarCipher.demovingShift(CaesarCipher.movingShift(u, 1), 1));
}
@Take a look at
public void test3() {
String u = "For you bouquets and ribbon'd wreaths--for you the shores a-crowding;";
assertEquals(u, CaesarCipher.demovingShift(CaesarCipher.movingShift(u, 1), 1));
}
@Take a look at
public void test4() {
String u = "Exult, O shores, and ring, O bells! However I, with mournful tread, Stroll the deck my Captain lies, Fallen chilly and useless. ";
assertEquals(u, CaesarCipher.demovingShift(CaesarCipher.movingShift(u, 1), 1));
}
@Take a look at
public void testa() {
String u = " uoxIirmoveNreefckgieaoiEcooqo";
Listing<String> v = Arrays.asList(" xscOp", "zvygqA", "ftuwud", "adaxmh", "Edqrut");
assertEquals(u, CaesarCipher.demovingShift(CaesarCipher.movingShift(u, 2), 2));
assertEquals(v, CaesarCipher.movingShift(u, 2));
}
@Take a look at
public void testb() {
String u = "uaoQop jx eh osr okaKv vzagzwpxagokBKriipmc U";
Listing<String> v = Arrays.asList("wdsVuw sh", " qu dii h", "evGs uzbi", "caudhoxuM", "Wewxfdu O");
assertEquals(u, CaesarCipher.demovingShift(CaesarCipher.movingShift(u, 2), 2));
assertEquals(v, CaesarCipher.movingShift(u, 2));
}
@Take a look at
public void testc() {
String u = "kgpiqislyhvmffdzlyehjiIteAaaotcoapk bbMgaHlda";
Listing<String> v = Arrays.asList("mjtnwpaui", "shztutqdr", "ycffGseBc", "dsyiviyu ", "noAvqYdwu");
assertEquals(u, CaesarCipher.demovingShift(CaesarCipher.movingShift(u, 2), 2));
assertEquals(v, CaesarCipher.movingShift(u, 2));
}
@Take a look at
public void testd() {
String u = "abcdefghjuty";
Listing<String> v = Arrays.asList("bdf", "hjl", "nps", "eek", "");
assertEquals(u, CaesarCipher.demovingShift(CaesarCipher.movingShift(u, 1), 1));
assertEquals(v, CaesarCipher.movingShift(u, 1));
}
@Take a look at
public void teste() {
String u = "abcdefghjuty1234";
Listing<String> v = Arrays.asList("bdfh", "jlnp", "search", "1234", "");
assertEquals(u, CaesarCipher.demovingShift(CaesarCipher.movingShift(u, 1), 1));
assertEquals(v, CaesarCipher.movingShift(u, 1));
}
}
``````