Click to See Code
public class App
{
public static void main(String args[])
{
//your method calls
System.out.println(encrypt("what is this", 6)); //call method encrypt
System.out.println(decrypt("q lwv'b mdmv" , 8)); //call method decrypt
}
//your methods here
public static String encrypt(String s, int key){
s = s.toLowerCase();
String encryptString = "";
String [] words = s.split(" ");
for (int x = 0; x < words.length; x++)
{
char [] chars = words[x].toCharArray();//chosen word to array
for (int i = 0; i < chars.length; i++){
if (chars[i] >= 97 && chars[i] <= 122){
if(chars[i] + key <= 122){
chars[i] += key;
}
else
chars[i] -= (26 - key);
}//if chars in range
encryptString += chars[i]; //adds char (letter / none)
}//end (int i = 0; i < chars.length; i++)
encryptString += " "; //adds character to string (letter/not)
}//end (int x = 0; x < words.length; x++)
return (encryptString);//return
}//end method encrypt
public static String decrypt(String s, int key){
s = s.toLowerCase();
String decryptString = "";
String [] words = s.split(" ");
for (int x = 0; x < words.length; x++)
{
char [] chars = words[x].toCharArray();
for (int i = 0; i < chars.length; i++){
if (chars[i] >= 97 && chars[i] <= 122)
if (chars[i] - key >= 97){
chars[i] -= key;
}//if chars in range
else{
chars[i] += (26 - key);
}
decryptString += chars[i]; //adds char (letter / none)
}//end (int i = 0; i < chars.length; i++)
decryptString += " "; //adds character to string (letter/not)
}// end (int x = 0; x < words.length; x++)
return (decryptString); //return
}//end method decrypt
}
Leave a comment
Log in with itch.io to leave a comment.