Welcome back.
I hope you enjoyed the cipher challenge.
And I hope you were successful with it.
And I hope you extended it even further.
If you did, post it on the forum, share what you did.
That's pretty fun stuff.
Okay, so what do we need?
First thing we need to do is ask the user to enter a secret message, right.
So let's do that first.
Let's knock that out before we even start writing any other code.
So we need a string object, and we can call that string
object secret message.
And we'll initialize it.
That's what the user is going to type in.
Okay, so I'm going to say cout, enter a secret message.
And we'll leave it, just like that.
And let me fix that typo and you know what let me just
put -- let me use proper English here enter your secret message.
Now the user is going to enter their message, and we need to read it.
It's potentially more than one word.
So we can't just use cin an extraction operator.
We're going to use getline.
And I want to get a line of text from cin, and I want to
store it in secret message.
Okay, so now at this point, secret message that string object will
contain whatever the user typed in.
Perfect.
So we want to encrypt that string.
Well, I need to store the encrypted message somewhere, right.
So let's create another string object, and we'll call it encrypted message.
And we'll initialize that as well.
That's going to hold the encrypted message once we finish encrypting it.
But at this point, it's just simply empty.
So we'll do a little output statement here just so we
can see some processing.
And we'll say something like encrypting message.
And we'll put an endline at the end.
All right, so now we start to process.
What do we need to do?
Well, there's a lot of different ways to do this.
There's the easy way, the harder way.
Let's talk about the harder way first.
Suppose that the message is Frank.
That's the secret message.
Well, I need to iterate through that string.
Obviously, I need to look at the f the r the a the n the k.
I've got to do that no matter what because I've got to look
at every single character.
Well, what I could do is I could read the first character that's the f.
Then I can loop through this array here, looking for that f.
And when I find it, I need to know what position it's in.
So I could have a counter that's counting up, right.
So that when I start at 0 and then I go the next one, next
one eventually, it's going to find it and I've got a position.
That's the hard way.
That's not the way I want to do it.
Remember, the string class has a method in it called find.
So if I want to find that character f, it's going to return the position of
f or it's going to return no position.
That's really all I care about.
Okay, so that's the way I'm going to solve this
problem, less code is better.
So let's solve this.
So right here, we're about to encrypt the message.
Now we need to iterate through the secret message
one character at a time.
So I'm going to use a range-based for loop here.
It's perfect for this.
So I'm going to say for, every character c in my secret message.
So now in the loop, I have access to c, which represents
the current character I'm looking at in my secret message, right.
So I'm going to call the find method in the alphabet string.
I'm going to ask the alphabet string to find c, right.
So if c is f, it's going to find that lowercase f and return its position.
Now that position's type is size t, just like we mentioned before.
That's an unsigned integer that's really dependent on your platform.
This will work everywhere.
We'll call it position.
And I'm going to ask the alphabet string or the alphabet object to
find that character in itself.
If it finds it, we're good.
We're going to substitute it with the equivalent character
at the same position and key.
If it doesn't find it, then we're just going to process that character as is.
It's not one of these.
So I'm not going to substitute it.
I'm just going to use it.
All right, so what do we need?
We need an if statement.
We're going to say if the position
is not equal to
no position, what does that mean, that means we found it, right.
Let's do the else.
And I'll just use a block else in case we need to add some code later.
Now if we didn't find it in here, we're not going to
substitute, we're just going to use the character as is, right.
So I'm going to say encrypted message + = plus equals c, just add the character
as is to the encrypted message.
We're not going to substitute it.
Now what if we do need to substitute it.
Let's go back here again.
We need a new character, right.
We need the character in the key.
So let's create a variable called new character.
And we can initialize this variable to the character in the string at
that position, the key string, right.
So we have position already.
So we can ask -- again, we can ask the key string to give us
the variable at that position.
And we can initialize it to that.
So now we've got the new character that we want to substitute.
And what do we do?
We simply say encrypted message + = plus equals new character.
That's it.
That should be all the code we need.
Now let's just output the encrypted message, so the encrypted message
would be the encrypted message, right.
So let's run this.
This is the first half.
This is just the encryption side.
But you can see the code is really easy to understand
very straightforward.
So let's run it, and let's say this is a test.
And that's the encrypted message right there.
Okay, now let's get -- let's do a simple test case.
We know that we're going to replace the lowercase a,
you can see it right here.
Let me use my pen real quick.
You can see that we're going to replace the lowercase
a with an uppercase x.
And it's obviously the capital z with an r.
So a good test case would be a a a z z z and we expect
back x x x r r r, right.
That's the encrypted.
So let's try that out.
So we'll do three lowercase a's and three uppercase z's.
A a a z z z.
And as I said, what we expect back is x x x lower case three r's.
That's exactly what we want.
Okay, so it looks like we're in the ballpark.
Obviously, you would have to test a lot better than this.
But that's a good example of a simple test case that tells
you you're definitely on track.
I'll erase this.
And let's try a couple more.
Let's try meet me at Bill's house.
Now in this case, you see we have an apostrophe.
We're not going to handle that apostrophe, right.
We're not substituting.
So we're just going to print it out as is.
So I expect to see that apostrophe in the encrypted
string in the same position.
So you can see here, there it is right there.
So that's correct.
All right, so that's the encrypted message.
Now let's decrypt the message.
Same idea, right.
We're just going the other way.
We need a string object for the decrypted message.
And we'll initialize that to an empty string as well.
We'll say cout, let's do another new line here, and
we'll say decrypting message.
All right, so now let's do the code again.
What are we doing this time?
Well, this time we're looping through the encrypted message, right,
grabbing those characters, finding them in the key and substituting
them back in the alphabet.
All right, so let's do it.
It's the same exact idea except we're going backwards here.
So for every character, let's call it c in the encrypted message this time.
For every character in there, we're doing exactly the same thing.
The position is we're going to ask the key this time to find that character.
And then if the position is not equal to no position,
then we found it, right.
The else part means just handle it as is, so let's
handle that part really easy.
So this time we're going to save this to the decrypted message, right.
So decrypted message + = plus equals c.
In this case, we did find it.
So we want to get a new character again, just like we did before.
Here's my new character.
And I want to initialize it to this time not the key but
the alphabets .at position.
So give me the character at the same position but in the alphabet string.
And I'll initialize it to that.
And then here, we'll just say decrypted message + = plus
equals new char.
Awesome. That's it.
Our string should be decrypted at this point.
And what we'll do here is we'll just say cout, put another new line
here in front, decrypted message.
And I'll fix that typo in a second.
In this case, it would be decrypted message, followed by a new line.
Okay, let's try this out.
Hopefully, we get back what we started with, right.
So enter your secret message.
This is a test.
I'm decrypt -- I'm encrypting first.
That's the encrypted message right here.
Then I'm decrypting, and I'm getting back this is a test.
Perfect.
Let's try a couple more.
Meet me at Bill's house at, let's say, 12:30 p.m.
Okay, this is a good test case because well, maybe
Nil's house, not Bill's house.
Meet me at Nil's house.
You see we have an apostrophe.
We have the 1 the 2, the colon and the 3 and the 0.
We're not handling those since they're not letters.
So we should see those characters as is in the encrypted string.
So let's try that.
There you go. There's the apostrophe.
There's the 12, the colon, the 30, just as we expected.
And then when we decrypt the message, we get back meet me
at Nil's house at 12:30 p.m.
Okay, now suppose we wanted to make this a little better -- not
a lot better, but a little better.
Let's deal with those spaces, right.
I want to fill up those spaces with something.
So what we could do is we could come up here to my alphabet.
And I can insert a space right at the beginning.
That's a space character.
Now what am i going to replace the space character with,
anything I want that's not already in here.
Let's say one of those characters, a left bracket.
That's fine.
But now we've got to deal with the left bracket, right.
So let's put the left bracket up here.
And we're going to replace that with a space because we've got to go both
ways or it's not going to work, right.
So now we're going to handle spaces.
At least, we'll fill up all that void space.
So let's try to run it.
And we'll do something like this is a test.
And you can see now, it's sticking those square brackets in there.
So it makes it look a little more jumbled up at least.
Obviously, this is so easy to break and then the decrypted message
is still correct, this is a test.
There's a lot of things you can do here.
There's some real famous ciphers that are substitution ciphers, like
the keyword cipher and the caesar cipher that you can play around with.
You can use random numbers.
We'll talk about generating random numbers so that maybe the first
time you see a space you replace it with something the second time
you replace it with something else that would just make it a little bit
more interesting and hard to break.
But there you go.
This is an example of using c++'s string class, very easy.
I mean, there's no counting.
There's no checking to make sure that things fit.
It's handling everything for me.
All right, so hopefully your solution is similar.
If it's not, you can learn from this one.
And if you've got a great solution, you can share it
with the rest of the class.
I'll see you in the next video.