Caeser Code

Pages: 12
Feb 4, 2015 at 7:40pm
I've been instructed to create two Caeser code programs. One that encrypts messages and the other that decrypts messages. The function return is bool and if the string contains non-alpha characters it will return false. Otherwise it is to return true and perform the encryption. Likewise for the decryption program.

I'm very unfamiliar with this type of programming and am at a bit of a loss as to what to do and how to approach this. Can anyone help me?
Feb 4, 2015 at 7:57pm
https://en.wikipedia.org/wiki/Caesar_cipher

Do you know how characters are represented by numbers?
Feb 6, 2015 at 2:39am
I know what the Caesar cipher is, I'm just not sure how I would go about programming this. My only guess right now would be a bunch of "if" statements, but that sounds convoluted, problematic, and probably not the best way to go about this.

And I'm not sure what you mean by how characters are represented by numbers.

Last edited on Feb 6, 2015 at 2:42am
Feb 6, 2015 at 3:47am
http://www.cplusplus.com/doc/ascii/

You can perform addition and subtraction with the char data type. Remember that character literals are of this data type.
Last edited on Feb 6, 2015 at 3:47am
Feb 6, 2015 at 4:01am
Unfortunately I'm required to use a string.
Feb 6, 2015 at 1:19pm
A string is composed of characters.
Feb 6, 2015 at 4:52pm
So it doesn't make a difference? How do you perform the addition and subtraction to the ascii and apply it to the letters exactly?

Also, I know I need to make a loop, but how would I measure the size of a string?
Last edited on Feb 6, 2015 at 5:32pm
Feb 6, 2015 at 6:10pm
1
2
3
4
5
6
7
8
std::string str;
std::getline(std::cin, str);
for(std::size_t i = 0; i < str.length(); ++i)
{
    char &c = str[i];
    //do manipulations to c
}
std::cout << str << std::endl;
Last edited on Feb 6, 2015 at 6:11pm
Feb 6, 2015 at 11:33pm
I see, but how to you add and subtract the ascii code and apply it to the string?
Feb 7, 2015 at 3:16am
Characters are numbers. Just do addition and subtraction as normal.

Here's a huge hint: ((((c-'a')+13)%26)+'a'
Feb 7, 2015 at 3:28am
OK, so if I subtract 'a', it's like subtracting 65, right? And what does % mean in this case? Sorry, I'm very new.
Feb 7, 2015 at 3:36am
% is modular operator, it divides by a int and the digit leftover is the remainder. For example, 10%3 would result in 1, because 1 is left over. Another example is 15/5, this would result in 0, as it has no remainder.
Feb 7, 2015 at 5:05am
OK...so assuming I'm correct that subtracting 'a' is like subtracting 65...if c were b, that would be 66-65, which would be 1. 1+13 is 14. 14/26, without being written as a decimal would be 0 with a remainder of 14...so since this is %, the end result would be 14, and we'd add 'a'(65) meaning the end result would be 89. Am I right or am I way off?
Feb 7, 2015 at 7:05am
The idea is to convert the letters into the range 0-25 and then rotate them for the Caeser Cipher, then get them back into the proper range again so they are letters and not random symbols.
Feb 7, 2015 at 8:29am
So do I declare them like this?

string a=0;
string b=1;
etc.

Or is there a more efficient way?
Feb 7, 2015 at 11:51am
You're on the right track. Given a character ch and a shift value s, you need to:
- Convert the character to a number representing it's position in the alphabet (0-25).
- Add the shift value s;
- Divide by 26 and take the remainder. This handles the case where the number wraps around from 25 to 0
- Convert the number back to a letter.
Remember to handle both upper and lower case.

You might put this in a function: char caesar(char ch, int s);
Once you have this working, you can encode a string by simply looping through the characters in the string and calling caesar() on each of them.


Feb 7, 2015 at 4:34pm
Alright, so I do declare them all individually?

When I have declared a string as a number, when I use that string ('a') in math it will automatically be a number ('0'), right?

Once I have the number response to an equation, how does that convert into a string?

How does this exclude none alphabetical letters?
Feb 7, 2015 at 6:05pm
CNerd2121 wrote:
So do I declare them like this?

string a=0;
string b=1;
etc.

Or is there a more efficient way?
CNerd2121 wrote:
Alright, so I do declare them all individually?
No, you do not need to do anything like this at all.
Feb 7, 2015 at 6:19pm
How do I give these each a specific number value then?
Feb 7, 2015 at 9:03pm
First, there is a difference between a char and a string.

A char is just a single-byte integer. (it might be signed or unsigned, but we'll ignore that for now). A constant like 'a' or '2' is a char whose ASCII value is the value for 'a' or '2'. In other words, these two statement are the same:
1
2
char ch = 65;  // 65 is the ASCII encoding for 'A'
char ch = 'A';


Now the compiler also lets you put together an array of chars that represent the ASCII encoding of a bunch of characters. For example, these are all the same
1
2
3
char *cp = "ABC";
char *cp = {'A', 'B', 'C', 0};
char *cp = {65, 66, 67, 0};


The 0 at the end of the last two a convention used by C and C++ to indicate the end of an array of chars when the chars represent ASCII characters. We call this a "null-terminated string" because it end with 0, which is often referred to as null.

Are you with me? The important thing here is that char is just a one-byte integer.

The char arrays I showed above have a big drawback: their size is fixed at compile time. Lots of real world problems require us to deal with strings of varying sizes. We add text to them, replace text, delete it, change it, etc. etc. Rather than making everyone write their own code for doing this (and frequently getting it wrong!), the Standard Library has a class that does all the details under the hood.

This class is string. It manages an array of char and handles all the memory management for you.

Now you can create a string like this:
string s = "This is a string";
and you can access each char within it. s[0] is the first character, s[3] is the 4th etc.

So I think you need to write your program from the bottom up. First, create a function that takes a char and a shift count, and does a caeser cypher shift of the char, by the shift count. I mentioned how to do this in a previous post.

Then create a function that takes a string and calls the caesar function on each character in the string.

Hope this helps.
Last edited on Feb 7, 2015 at 9:06pm
Pages: 12