Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Caesar Cipher

1. ## Caesar Cipher

I'm in an intro to programming class and while I'm able to follow along with basic problems, I totally don't get some of the problems that get assigned as homework. I've spent an inordinate amount of time trying to just figure out how to started on what I've come to realize (after looking at literally every google search possible related to this) is a very simple problem. I just don't know how to get the ball rolling here. I basically just need to make a program that takes a cipher inputted by the user and moves all the characters in that cipher "up" 3 characters [i.e. D (68 as I understand it is interpreted in computer language) goes to A (65)]. I realize it's really supposedly really simple but I have literally no idea how to do it.

I read the sticky thread on homework assignments and I'm not trying to just get the answers. I've got a whole semester in this class ahead of me and if I don't start getting this now I'm in for a fairly large ****storm. That said, can someone give me like a pseudocode or something so I can see the basic framework? The full code would be great as well but I really want to wrap my head around this stuff and I think seeing it in a language I can actually understand might do a lot to help. Any and all assistance would be extremely appreciated.

• It's a relatively simple matter. There are many ways, I'll describe one of the "cleaner" ways to get there despite being a bit more work for it.
In java, strings are little more than an object wrapper for a char array.
So the first thing you need to do (assuming CLI) is use an input technique. Look at using the Scanner class in Java, the API should have some pretty good examples of usage as well.
Once you've retrieved your string input, you then use the .toCharArray() function on it. This will give you a char[] as a result.
Use a for loop to iterate the char array. Subtract 3 from the char. You may need to handle the bounds of the ascii character to keep it on letters themselves (I don't know if that's a valid criteria or not, but that's simply a matter of knowing the min and max, and if you go past the min, wrap the difference by subtracting from the max instead).
With your new char, you can start assembling a new string. You can use concatenation or StringBuffer to do this.

And just like that. The hardest part will probably be the boundaries.

• Don't worry about the bounds or StringBuffer. This is pretty easy. Not tested but should work.

Code:
```Scanner sc = new Scanner(System.in);
System.out.println("Enter string: ");
char[] text = sc.nextLine().toCharArray();

String cipheredText = "";

for (char c: text)
{
cipheredText += Character.toString((char) (c + 3));
}

System.out.println("Ciphered string: " + cipheredText);

sc.close();```

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•