Hello! Welcome to the section 10 challenge.
In this challenge, what I'm asking you to do is to create
a substitution cipher program.
This is really straightforward, but it'll give you some experience
using the c++ string class.
Now there's an easy way to do this and a not so easy way to do this.
I'm not going to tell you what the easy way to do this is
because I want you to learn and try to figure it out on your own.
Let me talk about the challenge.
A simple very old method of sending messages is the substitution cipher.
I remember using this as a kid with some of my friends.
We used to have something called a secret decoder ring.
And the idea is you have a message and then you look at every character
in that message and you replace it with the character on the ring.
So for example, every time you see a lowercase a it gets replaced
with a capital x or every time you see a lowercase b it gets replaced
with a capital z and so forth.
So it's called the substitution cipher.
It's really a straightforward cipher.
It's not a very secure cipher at all, but it's a fun cipher to do.
So here's the idea.
I want you to write a program that asks the user to enter a secret
message, that'll be a string.
And then I want you to encrypt that message using the substitution cipher
and display the encrypted message.
Then take that encrypted message and decrypt it back
to the original message,
okay? You can use two strings for your substitution.
You can have one string for the alphabet.
And let me write this here for you that way you maybe
understand a little bit better.
So let's say you have one string which is the alphabet.
And this string contains all the characters that
you want to substitute.
So for example, let's say we've got a b c and so forth.
And capital a capital b capital c.
Okay, then I've got another string called the key.
And you can name these anything you like.
And the idea here is that for every character up in the alphabet string,
you're going to have a character in the key string that'll substitute it.
So if i want to substitute that lowercase a for an x, it'll be here.
And let's say that the b gets a z and the c gets a q and then this
uppercase a gets a lowercase, let's say, z, a lowercase q and
a lowercase d, it doesn't really matter what you replace them with.
So now the idea is really straightforward.
Whenever you see the secret message, let's say, the secret message is
a b c, you look in the alphabet.
You look for that little a, you find it.
You find it in position 0.
Then you go to the key, look in position 0, whatever
is there you replace it.
You do the same thing with lowercase b.
It's going to be replaced with the z.
And you do the same thing with the lowercase c, it'll
replace it with a q.
Okay, obviously you just keep going down the string, whatever you find,
you replace, so that would be my original message, this is now my
encrypted message. How would i decrypt the message? Do the same
thing just the other way around.
You look in the key for an x.
I find it at position 0.
So I go to position zero in my alphabet, and I grab the little a.
Again, I look for the z.
It's in position one.
There's my b in position one.
And then my q is going to be in position three or sorry, position two.
And it gives me my c.
So there you go.
Really, really straightforward to do.
Make the cipher stronger if you wish.
This is a really weak cipher.
Right now it's only substituting the letters.
And let me scroll down here just a little bit to show you what
I'm giving you to start with.
You can see what I'm giving you to start with.
I'm giving you an alphabet, which is a string of all the
lowercase and uppercase letters.
I kept these in order just to make it easier to debug.
And then you've got the key.
Those are the substitutions.
So for every one of these letters, there is an equivalent letter here
that we're going to substitute.
So for example whenever we see a lowercase a in our
message, we're going to substitute it with a capital x.
Whenever I see a capital z, I'm going to substitute it with a lowercase r.
That's for encryption.
And then for decryption, you just read the encrypted string
and you go the other way around.
So if you see a capital x in your encrypted string, you'll replace it
with a lowercase a to decrypt it.
That's it, really straightforward.
As I said, I'm only using letters here, I'm not dealing
with numbers or spaces.
You can be as creative as you want with that.
There's a lot of ways to make this a lot stronger
and just have fun doing it.
Remember, I'm writing this right here on line 28.
Remember, the less code you write, the less code you have to test.
You reuse existing functionality in the libraries.
Use the code that's in the stream class.
Don't try to create all this on your own.
Remember, the kiss principle, keep this as simple as you can, reuse
as much as you can and that makes your program very likely a lot
more correct than it will be if you try to do everything on your own.
Remember, those those library functions and methods that we've
used, they've been tested, and they've been debugged for years and years.
So they're pretty solid, so reuse those.
So let me give you a sample run so that you can see what to expect.
So I'm going to run this.
And you can see here, enter your secret message.
And I'll say I'll meet you at Billy's house at 10 a.m.
Okay, now press enter.
And it says encrypting message, and here's the encrypted message.
You can see it's replacing characters.
It's pretty obvious what's going on here, right.
You can see the capital i in isle is being replaced with a lowercase j.
The apostrophe is not being replaced at all because I'm
only dealing with letters.
The lowercase l is being replaced with the capital d, you can see here.
So you can see going across that that's your encrypted message.
Then I'm taking that encrypted message and decrypting it just doing the
opposite I just did, and I got back the same message I started with.
Okay, so you can see it's pretty easy.
So my secret message would be this is my secret message.
There's the encrypted message.
Okay, you can see the third character here is an i, which is
being replaced by a capital j.
And you can see the i again right here in the is, and
there's the capital j again.
Okay, so have fun, like I said and make this stronger
that's what I like to do.
You know learn and play around with ideas that you may
have to make this stronger.
It's a lot of fun to try.
Okay, so when you're done, I'll see you over in the solution,
and I'll show you my solution.