- #1
Hiche
- 84
- 0
Homework Statement
Homework Equations
The Attempt at a Solution
Code:
public class Caesar
{
public static void main(String[] args)
{
int k = Integer.parseInt(args[0]);
char c;
String strMsg = StdIn.readString();
System.out.print("Encrypted sentence is: " );
for (int i = 0; i < strMsg.length(); i++)
{
c = strMsg.charAt(i); // get characters
c = (char) (c + k); // shift characters of string k times
System.out.print(c);
}
System.out.println();
}
}
Okay, this works, well almost. Suppose that the shifting value is k = 3, then it will look like the original/caesar in the above image. If I want to decrypt (or encrypt? You get the point.) the message Wxyz, then the output will result in characters different than Zabc. Basically, the 'Hint' part is where I am having trouble making.
And is the code I wrote equivalent to the question/problem asked? I'm not positive I should input a String.
Last edited: