Question

I'm stuck in my homework which is about a caesar cipher java program which includes an...

I'm stuck in my homework which is about a caesar cipher java program which includes an infinite while loop, char array, and a switch statement

I have to encode and decode messages

Homework Answers

Answer #1

public class Main

{

public static int checkCharacter(char ch) {

if (Character.isUpperCase(ch)) {

return 1;

} else if (Character.isLowerCase(ch)) {

return 2;

} else {

return 3;

}

}

public static String encrypt(char chars[], int s)

{

StringBuffer result = new StringBuffer();

char ch;

int i = 0;

while(true)

{

if (i >= chars.length) {

break;

}

switch(checkCharacter(chars[i])) {

case 1:

ch = (char)(((int)chars[i] + s - 65) % 26 + 65);

result.append(ch);

break;

case 2:

ch = (char)(((int)chars[i] + s - 97) % 26 + 97);

result.append(ch);

break;

default:

break;

}

i ++;

}

return result.toString();

}

public static String decrypt(char chars[], int s)

{

StringBuffer result = new StringBuffer();

char ch;

int i = 0;

while(true)

{

if (i >= chars.length) {

break;

}

switch(checkCharacter(chars[i])) {

case 1:

ch = (char)(((int)chars[i] - s + 65) % 26 + 65);

result.append(ch);

break;

case 2:

ch = (char)(((int)chars[i] - s + 97) % 26 + 97);

result.append(ch);

break;

default:

break;

}

i ++;

}

return result.toString();

}

public static void main(String[] args)

{

String text = "ATTACKATONCE";

int s = 4;

System.out.println("Text : " + text);

String encrypted = encrypt(text.toCharArray(), s);

String decrypted = decrypt(encrypted.toCharArray(), s);

System.out.println("Encrypted text: " + encrypted);

System.out.println("Decrypted text: " + decrypted);

}

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Hi, I'm currently stuck with my homework. I need to know how to write this program...
Hi, I'm currently stuck with my homework. I need to know how to write this program in Python as below: Write function repeatPhrase(phrase, n), which prints the given phrase n times, alternating between lowercase and uppercase. For example, repeat('The sky is blue', 5) would print: the sky is blue THE SKY IS BLUE the sky is blue THE SKY IS BLUE the sky is blue Please help me, thank you very much!
I'm trying to figure out why my program has an infinite loop. I'm pretty sure it...
I'm trying to figure out why my program has an infinite loop. I'm pretty sure it has something to do with the catch(bad_alloc) function or maybe the while (true) loop above it but I'm not sure. Can you help me figure out why i have an infinite loop and help me fix it? Thanks ---------------------------------- main.cc ---------------------------- #include #include #include #include "numbers.h" using namespace std; int main() { Numbers N1, N2;       for(size_t i = 2; i < 16;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
I'm currently stuck on Level 3 for the following assignment. When passing my program through testing...
I'm currently stuck on Level 3 for the following assignment. When passing my program through testing associated with the assignment it is failing one part of testing.   Below is the test that fails: Failed test 4: differences in output arguments: -c input data: a b c -c expected stdout: b observed stdout: a b expected stderr: observed stderr: ./test: invalid option -- 'c' Unsure where I have gone wrong. MUST BE WRITTEN IN C++ Task Level 1: Basic operation Complete...
IN JAVA!! You may be working with a programming language that has arrays, but not nodes....
IN JAVA!! You may be working with a programming language that has arrays, but not nodes. In this case you will need to save your BST in a two dimensional array. In this lab you will write a program to create a BST modelled as a two-dimensional array. The output from your program will be a two-dimensional array.   THEN: practice creating another array-based BST using integers of your choice. Once you have figured out your algorithm you will be able...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
Hi there, I've been asked to write a program in C which can read values from...
Hi there, I've been asked to write a program in C which can read values from a file then sort them, and then write to a binary file. I'm getting stuck when I write my binary file as the output is just spitting out garbage values and not the values that are being read in. When I print my input file reader everything is perfect but after sorting and then writing, the output is completely wrong. I have checked that...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...
C++ PROGRAMMING Hi! I have to make a program that adds fractions and simplifies them. I...
C++ PROGRAMMING Hi! I have to make a program that adds fractions and simplifies them. I feel like I know how to write that. What I'm having trouble with is implementing two files the professer gave us. I would appreicate any help in understanding their purpose as in if Im supposed to take information from those files or give it information. Thank you! I have attatched the homework instructions and the two files given. Implementation The main program, called calculator.cpp...
Strings The example program below, with a few notes following, shows how strings work in C++....
Strings The example program below, with a few notes following, shows how strings work in C++. Example 1: #include <iostream> using namespace std; int main() { string s="eggplant"; string t="okra"; cout<<s[2]<<endl; cout<< s.length()<<endl; ​//prints 8 cout<<s.substr(1,4)<<endl; ​//prints ggpl...kind of like a slice, but the second num is the length of the piece cout<<s+t<<endl; //concatenates: prints eggplantokra cout<<s+"a"<<endl; cout<<s.append("a")<<endl; ​//prints eggplanta: see Note 1 below //cout<<s.append(t[1])<<endl; ​//an error; see Note 1 cout<<s.append(t.substr(1,1))<<endl; ​//prints eggplantak; see Note 1 cout<<s.find("gg")<<endl; if (s.find("gg")!=-1) cout<<"found...