0:03

We would like to write a function which can compare

Â two strings to see if they have the same contents.

Â That is, if they are the same sequence of characters.

Â As with all programming problems,

Â we can follow the seven steps to solve this problem.

Â We'll start with step one and do an instance of the problem ourselves.

Â Here we have two strings.

Â Notice how we have represented them quite

Â explicitly including the null terminator character,

Â backslash zero, at the end of each.

Â We're going to start looking at the first letter of

Â the first string and the first letter of the second string.

Â These are both capital A.

Â So we want to look at the next letter.

Â These are both P as are both letters in the next string.

Â Then we have Ls in both strings and Es both in strings.

Â However, now we have a null terminator in the one string and an S in the other.

Â These strings are not the same,

Â so we would give an answer of no.

Â Okay. So what exactly did we do?

Â First, we made this arrow to keep track of where we are in string one.

Â It points at the first letter of string one.

Â It is generally a good idea to give things names,

Â so let us call it P1.

Â We also made an arrow to keep track of where we are and

Â string two which we might want to call P2.

Â Next, I checked if the letter that P1 points at match the letter that P2 points at,

Â both of these were capital A.

Â So I advanced P1 to the next letter after it,

Â and I advanced P2 to the next letter after it.

Â Then I checked if the letter that P1 points at matched the letter that P2 points at.

Â Both were P, and so I advanced P1 to the next letter and advanced P2 to the next letter.

Â And again, I compared the letters that P1 and P2 pointed at,

Â and I found them to be the same.

Â So I advanced P1 and P2.

Â Does this seem a bit repetitive?

Â If so, that is great,

Â since finding repetition is one of

Â the important parts of generalizing which we will do in just a minute.

Â First, we should finish step2.

Â Again, we compare the letters P1 and P2 point at and

Â find them the same so we advanced P1 and advanced P2.

Â P1 and P2 both points at E so we advance P1 and advance P2.

Â However, now the letters that P1 and P2 point at are different.

Â One is a null terminator and The other is S. So at this point,

Â we decide that our answer is no.

Â These strings are not the same.

Â Here are all the steps that we just did.

Â So now we should generalize them to work on any two strings.

Â These first two steps are initialization that we will always do.

Â No matter what two strings we want to compare,

Â we always want to set up P1 and P2 to point at their first letters.

Â As we noted earlier,

Â these next steps are quite repetitive.

Â We keep checking if the two letters are the same and advancing P1 and P2.

Â When do we stop repeating these steps?

Â When the letters that P1 and P2 point at are different.

Â With that observation in mind,

Â we have rewritten our steps to be a bit more general.

Â Here we have expressed these steps as repetition that we

Â do as long as P1 and P2 point at the same letter.

Â These steps are pretty general.

Â We expressed the similar steps as

Â repetition and we aren't using any values particular to the problem.

Â However, we are missing something very important.

Â Can you spot any warning signs that something is wrong with our algorithm?

Â This algorithm always gives an answer of no, never yes,

Â that comes from the fact that we only worked one example with

Â a no answer and have not thought about how to get a yes answer yet.

Â We should be aware of the fact that we never

Â explicitly checked for the end of the string.

Â How could we fix these problems?

Â We can go back to step one and work through

Â another instance of the problem that gives us a yes answer such as this one.

Â Both strings are Apple.

Â We aren't going to work through it in detail,

Â but we will note that we would realize they are the same when

Â we reach the end and have not found any mismatching characters.

Â How do we know when we have reached the end of the string?

Â We know we have reached the end of the string when we encounter

Â the null terminator character, backslash zero.

Â We can adjust our algorithm based on this observation.

Â If P1 points at backslash zero,

Â then we have reached the end of string zero.

Â Do we also need toa check if P2 points at backslash zero?

Â It would not hurt anything but we just checked if P1 and P2 point at the same letter.

Â So if P1 points at backslash zero,

Â then P2 must also point to backslash zero here.

Â Notice that our algorithm now explicitly checks for the end of the string

Â which was the other warning sign we worried about.

Â Next you would want to test this out.

Â We're going to ally that step in this video,

Â but we encourage you to try it out and convince yourself that it is right.

Â Now lets translate this algorithm into code.

Â As usual, we start with the declaration of the function.

Â This function is called string equal and it takes two const char stars.

Â Why do we have const in the parameter types?

Â Because we want to promise that this function does not modify the strings passed in.

Â This function returns an int which will be zero for no and one for yes.

Â The first step says that we made an arrow pointing at the first letter of string one.

Â This line turns into a variable declaration.

Â P1 is a new variable which is initialized to a string

Â one so that it points at the same place that string one points.

Â The type of P1 is also const char star since P1 is

Â a pointer to a character but we will not modify the character that it points at.

Â Likewise, the next step is the declaration and initialization of P2 to string two.

Â Next we want to repeat some steps as long as a condition is true.

Â Repeating something as long as a condition true translates into a while loop.

Â For the condition inside the while loop,

Â we need to test if the letter P1 points

Â at is the same as the letter that the two points at.

Â We want star P1 equals equals star P2.

Â Next we want to check if P1 points at the null terminator.

Â So we have an if statement whose then clause

Â will give a yes answer which would be return one.

Â After this statement, we advance P1 to the next letter.

Â How can we make a pointer point at the next element of a sequence?

Â We can just write P1 plus plus or P1 equals P1 plus one.

Â Likewise, we can increment P2 to make it point at the next letter in the second string.

Â After the loop, we would give an answer of no which would just be return zero.

Â Great. Now we have written code to compare two strings and see if they are equal.

Â We should then test this code and see if it works correctly.

Â