'How to keep spaces for the encypted text
class Main {
public static void main(String[] args) {
String alphabet = "abcdefghijklmnopqrstuvwxyz";
String message = "how are you";
int shift = -3;
System.out.println(message);
String eMessage = "";
if(shift < 0) {
int newShift = shift % 26 + 26;
for (int i = 0; i < message.length(); i++) {
int wordPosition = alphabet.indexOf(message.charAt(i));
int numCipher = (wordPosition + newShift) % 26;
char cipher = alphabet.charAt(numCipher);
eMessage = eMessage + String.valueOf(cipher);
else {
int newShift = shift % 26;
for (int i = 0; i < message.length(); i++) {
int wordPosition = alphabet.indexOf(message.charAt(i));
int numCipher = (wordPosition + newShift) % 26;
char cipher = alphabet.charAt(numCipher);
eMessage = eMessage + String.valueOf(cipher);
}
}
System.out.print(eMessage);
}
}
It works for single words, but when i type a full sentence the encrypted version of the message does not keep any spaces. It instead changes the spaces into w's. How do I keep the spaces for full sentences? Example of what I need- input: how are you, output: elt xob vlr ---- Example of what I get - input:how are you, output: eltwxobwvlr
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
