For some reason this piece of code will only encrypt one letter and wont loop back.
Can someone suggest a way around this?
There is another file that calls this method, there is no problem with that file.
Can someone suggest a way around this?
There is another file that calls this method, there is no problem with that file.
Code:
public class CaesarCipher {
private int key;
public CaesarCipher( int inKey ) {
// Initialise key
key = inKey;
}
public String encrypt( String s ) {
// Encrypt the argument a character at a time & return the result
for (int i = 0; i<s.length(); i++){
char letter = s.charAt(i);
char newLetter = decrypt(letter);
char [] data = {newLetter};
String end = new String( data );
return(end);
}
return(s);
}
public String decrypt( String s ) {
// Decrypt the argument a character at a time & return the result
for (int i = 0; i<s.length(); i++){
char letter = s.charAt(i);
char newLetter = decrypt(letter);
char [] data = {newLetter};
String end = new String( data );
return(end);
}
return(s);
}
private char encrypt( char c ) {
// Encrypt the argument & return the result
if ((char)(c + key)>90 && (char)(c + key) < 97){
return (char)(c + key - 26);
}else
if ((char)(c + key)>123){
return (char)(c + key - 26);
}
return (char)(c + key);
}
private char decrypt( char c ) {
// Decrypt the argument & return the result
if ((char)(c - key)>90 && (char)(c - key) < 97){
return (char)(c - key + 26);
}else
if ((char)(c - key)<65){
return (char)(c - key + 26);
}
return (char)(c - key);
}
}