FatherPhi video
CS50 Caesar Cipher Solution - PSET 2 | 2021 Step by Step Tutorial | Live Coding
22.5K views ยท 208 likes ยท 102 comments
About this video
Vote for the next video here
๐๐๐
https://FatherPhi.com
Eleven labs is the crazy ai voice generation tool I use to fool ChatGPT
Try it out for free!๐ https://try.elevenlabs.io/6wbvspyzcq44
(Affiliate link)
For the weekly brief on accessible and practical Ai usage + updates on our latest mission join the newsletter
๐ https://master-of-ai.com
๐ฉ For business inquiries please reach out to phi@fatherphi.com
And since people are asking.
The camera I use to shoot these videos
๐น DJI Osmos Pocket 3 ๐ https://amzn.to/3O3Qw8R
Transcript
today we're going to tackle the Caesar
cipher as far as cs50 alright let's head
to code
all right so I didn't see this diaper
and here is just describing to us what
we're gonna be doing the basic where the
Caesar cipher works just your shifting
whatever letter you're focusing on by
the key that you're given so this looks
pretty cryptic I pretty much just came
up with something on my own
this probably works better than what I
did I basically just went in and did it
in a dumbest way possible but it works
so I'm gonna show you how but first
we're going to go to the pseudocode
enlarge it a little bit alright so here
we go
the main problems were gonna face are
getting a key from the user and
encrypting or and breaking this down
further we have to get the key from the
user store that key validate the key
take the string input from the user
store the string validate the string
from the user and on the case where the
key is small enough to just tack on and
handle the case where the key is larger
than alphabets so we have to wrap around
and then actually encrypting the word
and printing it out to the screen face
these includes that's what these are the
libraries we're gonna need oh it didn't
pull in the cs50 page it's like I said
quick and dirty but it works we're gonna
start with the main int arc CD you know
it's dirty whenever you start off at the
main there's no functions involved so
start off with the if statement we're
gonna check if arc C is equal to two if
it's not equal to two we're going to
print an error message okay
we have to check the bits too because we
need two arguments for this to all work
and if the user isn't giving us two
arguments then we might as well just you
know give them the error message and not
waste our time running the rest of the
program so this error message is going
to be usage Caesar key and the new line
it's just what cs50 tells us that they
want to see they also want to see us
return of a value of one okay next we're
going to be doing an else--if statement
to catch the cases that we do have two
arguments and if that's the case we can
move into this state statement so the
terms for this whole like section will
probably it will probably benefit from
being a function but it checks our key
for validity aka
but first we convert index 1 RV to an
int a to I is the function will be using
and it's our standard library H and it
age and it returns an integer I'll be
strength
so the way we're gonna start this off is
we're gonna make a constant and key I
using a constant because I'm not
expecting the key to change so a to I
our v1 so as you can see here we're
taking the index of one of our bean
we're not taking zero because zero would
be Cesar and our and the one is the
actual number that they're putting in
after the Cesar so then we're going to
store a boolean to use an off switch
like if we detect a bad input so the
reason we do this is will come clear as
we keep going here so we're gonna have
the g-hold is keep Alvin we're gonna set
it as true so we're just going to assume
that the key is true for now so we're
gonna check it just in just a bit so to
check it we're gonna have to store the
lint our key we're gonna do that with an
integer string granese function
stranglin from the string h library and
we're gonna take the lens RB RB 1 so
it's gonna return a number and give it
to Lynn so now we're going to loop that
checks each digit to see you bits number
so four and I equals zero
we're started the Euro I less than Lin
says on his eyes less than the lint I
just go to run one second see once it
becomes equal to length we break out of
this loop and what are we going to be
doing in each iteration checking each
left number so let's say the user gives
us a key of 24 that's two digits so
we're gonna check two
and we're gonna check for because it's
actually a string right now so a string
is just an array of the those numbers so
the first the zero index is going to be
two in that situation and the one index
is gonna be four - four becoming 24 so
if and we're gonna pull a function from
another library we're going to be doing
this a lot if is digit which is part of
C type age detects a non digit it'll set
our stored boolean to false and it will
in the loop because we don't need to
keep going at that point so we're going
to actually call this digit here get
we're gonna feed is digit Arg V 1 but
we're of rd1 we're gonna take I and if
it is false
as in his digit returns false well set
art is key valid to false as well and in
plain English you can just see that the
key ears set to false so that would mean
that we would have to start over and ask
for the input again but I want to get
back to this 2d array right here because
if you think about it the array an array
is basically a one-dimensional rectangle
so it still has that dimension of one
like in height and then the I would be
the length of it so to access that each
index of the string you have to call you
have to use the second bracket right
here so now to break like we said
earlier in the loop I'm just gonna set I
equal to length that'll break us out of
the loop or I guess I could break or
something this is how I did it so now
we're gonna get out of the for loop yep
I'm gonna do another statement to check
to move on it's a key is valid so if the
key is valid we're going to
ask them for the plain text and we're
gonna store this in a string called
plain the string get a string playing
get string that's what I used can name
it whatever you want but this is
mandatory you have to type it like this
for cs50 and then after that you're
going to take the length of payment of
the plain text and this stores it in an
integer so we can use later I think at
this point I didn't even know what what
I was gonna do with it I just saved it
just because I knew thousand a number
that I might use later which I mean
which we got nothing so here we're gonna
open up a for loop to start going
through the letters of the plain of the
plain text so we're gonna start getting
used to these four loops it's the
structure of it setting an integer from
about declaring an integer I setting it
equal to zero and we're just gonna go
through the entire length of the plain
text by using that variable we created
all right the fun part is here so like I
said no pseudocode what we're gonna need
to actually we have to add to it
we're not to handle let's do eight a sum
with everything reno chrome endl upper
and lower case
so it's plan for that now so we're gonna
first we get a check like going from the
bigger problems down to the smaller I'm
gonna check to see if it's upper case or
lower the lower case and the way we do
this is we're going to use the function
from scene type so if is upper plane
index of I so we're starting at 0 here
so plane so the first letter of playing
if it's if it's upper case it's going to
return either true or false and if it's
true it'll go into here from here we can
set up the stage for the lower one too
so if is lower it of plane of I for list
of the elsif if that's true then it'll
go through that go to this body right
here alright so after we've done that
we're gonna do some a schema slowly a
ski mask works as you can do like Z - oh
yes to use folks so X Z minus a that
will return you good number and you can
just look up on ASCII table I got just
Google ASCII table and you'll you'll
find all the values I think a is like 97
or 96 but we can let the computer do
element of force like David Malin said
so here we can do what I did was I used
plain I added it to the key that we were
given
and I check if it's greater than Z
Capital Z
so in this situation if the plane is
that if that letter is plus the key is
larger than capital Z that means we have
to do with some wrapping around can't
just tack on the key and expect
computers just no Oh like let's say we
had 22 Y that's gonna bring it to the
lowercase letters or like symbols or
whatever you know I mean so we have to
make it wrap around the alphabet and so
to do that we're gonna have to again a
an in remain like a he remainder of some
sort I'm gonna track the number that
wraps around but I did was I stored a
key remainder as an integer using this
little formula right here so what this
does is it just adds the key to the to
the letter letters ASCII value and then
we subtracted e from them so that'll
give us that'll take away one alphabets
worth and then give us the like the
number that went past the Z
so now we check see exactly remainder is
greater than the entire length of the
alphabet still that's a pretty big
number so it's still bigger than the
alphabet we're gonna have to use
somewhat like kind of like modular math
but I just use a big while loop so well
Z maliki remainder is still larger than
the entire length of alphabet is going
to just subtract the entire alphabet
from that number until it can fit inside
the entire alphabet
so once it can fit inside the entire
once we don't need to worry about the
wrapping anymore we check just the plain
text for the letter that we're focusing
on plus the key remainder is still
greater than Z because like I said it
could be Y and and we're adding 20 cuz
20 still less than they're tired off
about Clint so we'll take the key
remainder and make it equal to plain
text left the plain letter plus RT
remainder so we'll subtract
Zini from it one last time and we'll
just add that we'll just tap that on to
Inc all right we definitely has a
remember to subtract one because if we
add on to a then we're not really
counting eight it's kind of like
starting a zero so if we subtract one
then we include a in the equation
okay so Ellis
now we're gonna handle the case of a key
remainder was already a manageable
number as we saw above we didn't have a
manageable number and we had to make it
manageable so we can just go ahead and
it to a to shift into its encrypted form
again we'll need the one because when
you start for me so we're gonna do else
lane I is equal to a plus Q remainder -1
so this will just add the key remainder
to a must be capitalized all right so
now I'm gonna move on to this situation
where our letter from plane in the plane
array plus key remainder mister guard
was never greater than the in the first
place so now we can just tack on the key
remainder like a path but we didn't have
but in this one we don't have to go
through the loop of whittling down the
key remainder until it's less than the
distance from distance from A to Z
and then yet another scenario here we
set it for the simplest case the letter
we're focused on can simply be added to
the given key give us our encrypted
letter I think I got that right
I'm pretty anal about my intention as
you can see okay so here let's do the
same thing yes I thought it'd be cool to
show you guys a very bad implementation
of this because I mean I'm not
embarrassed on my credit you know but
it's also good to see for beginners I
think is we can't all see in terms of
like zeros and ones basically so here in
simplest case is handled and we can move
on to the lower case which the lower
case is very much the same thing I can
just copy and paste the whole thing from
here
pasted here this mouse is so garbage
okay
so we'll just start replacing the
uppercase letters with lowercase
right
I know it's gonna be difficult to get
this car here
oh right here because now we're done
with the lower case as well and this
else statement is going to catch
anything else that goes wrong honestly I
just put this in because the idea he was
throwing me errors I mean the check 50
will start giving me an error saying
that there was some case maybe that it
didn't return this but it passed the
check 50 so it wouldn't it would really
tell me you know what I mean
seriously this mouse is so frustrating
so after this
sets into this that is key valid for
loop
you're going to printf the ciphertext
I'm inferring it just like check trustee
wants us to then underneath that we're
going to do an else statement or any
other cases that I did not foresee print
out the error again all right so let's
let's copy this paste in I'm gonna write
check this way to make it okay have the
error on line 44
okay I've been doing Python so so on
line 18 by 14 we have to pick without a
semicolon here
and v 984 120 to 130 184 132 122 131
okay try making again I compiled cross
my fingers let's try let's try this
hi all right we got JK so let's see the
check 15 to see if it works okay so I'm
gonna show you guys the error I made and
why that bar food thing didn't work so
we're gonna have to go to my while loops
we're all subtracting this length of the
alphabet and finally Z minus a is in 26
so that's where we went wrong okay 26
here and then see the runs makes Heiser
compiles then we're gonna check 50 yet
again all right there we go I'm gonna
include my copy my code from the github
in my description below so check it out
if you need to follow along and good
luck thanks for watching through or
skipping to this point in the video if
you like if you like my content like or
subscribe and I would appreciate it very
much thanks
cipher as far as cs50 alright let's head
to code
all right so I didn't see this diaper
and here is just describing to us what
we're gonna be doing the basic where the
Caesar cipher works just your shifting
whatever letter you're focusing on by
the key that you're given so this looks
pretty cryptic I pretty much just came
up with something on my own
this probably works better than what I
did I basically just went in and did it
in a dumbest way possible but it works
so I'm gonna show you how but first
we're going to go to the pseudocode
enlarge it a little bit alright so here
we go
the main problems were gonna face are
getting a key from the user and
encrypting or and breaking this down
further we have to get the key from the
user store that key validate the key
take the string input from the user
store the string validate the string
from the user and on the case where the
key is small enough to just tack on and
handle the case where the key is larger
than alphabets so we have to wrap around
and then actually encrypting the word
and printing it out to the screen face
these includes that's what these are the
libraries we're gonna need oh it didn't
pull in the cs50 page it's like I said
quick and dirty but it works we're gonna
start with the main int arc CD you know
it's dirty whenever you start off at the
main there's no functions involved so
start off with the if statement we're
gonna check if arc C is equal to two if
it's not equal to two we're going to
print an error message okay
we have to check the bits too because we
need two arguments for this to all work
and if the user isn't giving us two
arguments then we might as well just you
know give them the error message and not
waste our time running the rest of the
program so this error message is going
to be usage Caesar key and the new line
it's just what cs50 tells us that they
want to see they also want to see us
return of a value of one okay next we're
going to be doing an else--if statement
to catch the cases that we do have two
arguments and if that's the case we can
move into this state statement so the
terms for this whole like section will
probably it will probably benefit from
being a function but it checks our key
for validity aka
but first we convert index 1 RV to an
int a to I is the function will be using
and it's our standard library H and it
age and it returns an integer I'll be
strength
so the way we're gonna start this off is
we're gonna make a constant and key I
using a constant because I'm not
expecting the key to change so a to I
our v1 so as you can see here we're
taking the index of one of our bean
we're not taking zero because zero would
be Cesar and our and the one is the
actual number that they're putting in
after the Cesar so then we're going to
store a boolean to use an off switch
like if we detect a bad input so the
reason we do this is will come clear as
we keep going here so we're gonna have
the g-hold is keep Alvin we're gonna set
it as true so we're just going to assume
that the key is true for now so we're
gonna check it just in just a bit so to
check it we're gonna have to store the
lint our key we're gonna do that with an
integer string granese function
stranglin from the string h library and
we're gonna take the lens RB RB 1 so
it's gonna return a number and give it
to Lynn so now we're going to loop that
checks each digit to see you bits number
so four and I equals zero
we're started the Euro I less than Lin
says on his eyes less than the lint I
just go to run one second see once it
becomes equal to length we break out of
this loop and what are we going to be
doing in each iteration checking each
left number so let's say the user gives
us a key of 24 that's two digits so
we're gonna check two
and we're gonna check for because it's
actually a string right now so a string
is just an array of the those numbers so
the first the zero index is going to be
two in that situation and the one index
is gonna be four - four becoming 24 so
if and we're gonna pull a function from
another library we're going to be doing
this a lot if is digit which is part of
C type age detects a non digit it'll set
our stored boolean to false and it will
in the loop because we don't need to
keep going at that point so we're going
to actually call this digit here get
we're gonna feed is digit Arg V 1 but
we're of rd1 we're gonna take I and if
it is false
as in his digit returns false well set
art is key valid to false as well and in
plain English you can just see that the
key ears set to false so that would mean
that we would have to start over and ask
for the input again but I want to get
back to this 2d array right here because
if you think about it the array an array
is basically a one-dimensional rectangle
so it still has that dimension of one
like in height and then the I would be
the length of it so to access that each
index of the string you have to call you
have to use the second bracket right
here so now to break like we said
earlier in the loop I'm just gonna set I
equal to length that'll break us out of
the loop or I guess I could break or
something this is how I did it so now
we're gonna get out of the for loop yep
I'm gonna do another statement to check
to move on it's a key is valid so if the
key is valid we're going to
ask them for the plain text and we're
gonna store this in a string called
plain the string get a string playing
get string that's what I used can name
it whatever you want but this is
mandatory you have to type it like this
for cs50 and then after that you're
going to take the length of payment of
the plain text and this stores it in an
integer so we can use later I think at
this point I didn't even know what what
I was gonna do with it I just saved it
just because I knew thousand a number
that I might use later which I mean
which we got nothing so here we're gonna
open up a for loop to start going
through the letters of the plain of the
plain text so we're gonna start getting
used to these four loops it's the
structure of it setting an integer from
about declaring an integer I setting it
equal to zero and we're just gonna go
through the entire length of the plain
text by using that variable we created
all right the fun part is here so like I
said no pseudocode what we're gonna need
to actually we have to add to it
we're not to handle let's do eight a sum
with everything reno chrome endl upper
and lower case
so it's plan for that now so we're gonna
first we get a check like going from the
bigger problems down to the smaller I'm
gonna check to see if it's upper case or
lower the lower case and the way we do
this is we're going to use the function
from scene type so if is upper plane
index of I so we're starting at 0 here
so plane so the first letter of playing
if it's if it's upper case it's going to
return either true or false and if it's
true it'll go into here from here we can
set up the stage for the lower one too
so if is lower it of plane of I for list
of the elsif if that's true then it'll
go through that go to this body right
here alright so after we've done that
we're gonna do some a schema slowly a
ski mask works as you can do like Z - oh
yes to use folks so X Z minus a that
will return you good number and you can
just look up on ASCII table I got just
Google ASCII table and you'll you'll
find all the values I think a is like 97
or 96 but we can let the computer do
element of force like David Malin said
so here we can do what I did was I used
plain I added it to the key that we were
given
and I check if it's greater than Z
Capital Z
so in this situation if the plane is
that if that letter is plus the key is
larger than capital Z that means we have
to do with some wrapping around can't
just tack on the key and expect
computers just no Oh like let's say we
had 22 Y that's gonna bring it to the
lowercase letters or like symbols or
whatever you know I mean so we have to
make it wrap around the alphabet and so
to do that we're gonna have to again a
an in remain like a he remainder of some
sort I'm gonna track the number that
wraps around but I did was I stored a
key remainder as an integer using this
little formula right here so what this
does is it just adds the key to the to
the letter letters ASCII value and then
we subtracted e from them so that'll
give us that'll take away one alphabets
worth and then give us the like the
number that went past the Z
so now we check see exactly remainder is
greater than the entire length of the
alphabet still that's a pretty big
number so it's still bigger than the
alphabet we're gonna have to use
somewhat like kind of like modular math
but I just use a big while loop so well
Z maliki remainder is still larger than
the entire length of alphabet is going
to just subtract the entire alphabet
from that number until it can fit inside
the entire alphabet
so once it can fit inside the entire
once we don't need to worry about the
wrapping anymore we check just the plain
text for the letter that we're focusing
on plus the key remainder is still
greater than Z because like I said it
could be Y and and we're adding 20 cuz
20 still less than they're tired off
about Clint so we'll take the key
remainder and make it equal to plain
text left the plain letter plus RT
remainder so we'll subtract
Zini from it one last time and we'll
just add that we'll just tap that on to
Inc all right we definitely has a
remember to subtract one because if we
add on to a then we're not really
counting eight it's kind of like
starting a zero so if we subtract one
then we include a in the equation
okay so Ellis
now we're gonna handle the case of a key
remainder was already a manageable
number as we saw above we didn't have a
manageable number and we had to make it
manageable so we can just go ahead and
it to a to shift into its encrypted form
again we'll need the one because when
you start for me so we're gonna do else
lane I is equal to a plus Q remainder -1
so this will just add the key remainder
to a must be capitalized all right so
now I'm gonna move on to this situation
where our letter from plane in the plane
array plus key remainder mister guard
was never greater than the in the first
place so now we can just tack on the key
remainder like a path but we didn't have
but in this one we don't have to go
through the loop of whittling down the
key remainder until it's less than the
distance from distance from A to Z
and then yet another scenario here we
set it for the simplest case the letter
we're focused on can simply be added to
the given key give us our encrypted
letter I think I got that right
I'm pretty anal about my intention as
you can see okay so here let's do the
same thing yes I thought it'd be cool to
show you guys a very bad implementation
of this because I mean I'm not
embarrassed on my credit you know but
it's also good to see for beginners I
think is we can't all see in terms of
like zeros and ones basically so here in
simplest case is handled and we can move
on to the lower case which the lower
case is very much the same thing I can
just copy and paste the whole thing from
here
pasted here this mouse is so garbage
okay
so we'll just start replacing the
uppercase letters with lowercase
right
I know it's gonna be difficult to get
this car here
oh right here because now we're done
with the lower case as well and this
else statement is going to catch
anything else that goes wrong honestly I
just put this in because the idea he was
throwing me errors I mean the check 50
will start giving me an error saying
that there was some case maybe that it
didn't return this but it passed the
check 50 so it wouldn't it would really
tell me you know what I mean
seriously this mouse is so frustrating
so after this
sets into this that is key valid for
loop
you're going to printf the ciphertext
I'm inferring it just like check trustee
wants us to then underneath that we're
going to do an else statement or any
other cases that I did not foresee print
out the error again all right so let's
let's copy this paste in I'm gonna write
check this way to make it okay have the
error on line 44
okay I've been doing Python so so on
line 18 by 14 we have to pick without a
semicolon here
and v 984 120 to 130 184 132 122 131
okay try making again I compiled cross
my fingers let's try let's try this
hi all right we got JK so let's see the
check 15 to see if it works okay so I'm
gonna show you guys the error I made and
why that bar food thing didn't work so
we're gonna have to go to my while loops
we're all subtracting this length of the
alphabet and finally Z minus a is in 26
so that's where we went wrong okay 26
here and then see the runs makes Heiser
compiles then we're gonna check 50 yet
again all right there we go I'm gonna
include my copy my code from the github
in my description below so check it out
if you need to follow along and good
luck thanks for watching through or
skipping to this point in the video if
you like if you like my content like or
subscribe and I would appreciate it very
much thanks