>> SPEAKER 1: Let's now write a program they truly compares two strings
character for character.
Let's use compare 0 as our starting point.
But let's first peel back the layer that is the string type and rewrite it
for what it truly is, which is a char star.
That is the address of a character, specifically the first character in a
sequence of characters that we'd more generally know as a string.
>> Same for t.
Let's rewrite that string declaration as char star.
And now we need to no longer compare s against t, lest
we compare two addresses.
We want to truly compare the two strings themselves.
To do this, we can use a function declared in string.h.
So I'll add that include on top of my file.
>> And then I'm going to change this line here.
Rather than compare s against t, I'm going to call a function stir comp,
for string compare, and pass in as arguments s and t.
We'll defer then to string compare to figure out if s and t are indeed equal
and let it figure out how to compare them character for character.
>> Now, according to the documentation for string compare, it's actually
going to return 0 if the two strings pointed at by s and t are the same.
It's going to return a negative number if s should come before t
alphabetically or a positive number if s should come after t alphabetically.
>> But for now, we only care about equality.
So I'm going to simply test if the return value of string compare,
passing in s and t, equals 0.
And if so, I'm going to claim that the two strings are the same.
>> But I'm going to make one other change as well.
It turns out that get string, per its documentation, can sometimes return
null, a sentinel value that, according to get strings documentation, means
something bad happened.
For instance, we ran out of memory or the user somehow didn't cooperate.
>> String compare, meanwhile, is a little fragile.
If you pass it null for either its first or its second argument, bad
things can happen.
Bad things usually involving segmentation faults.
So to avoid that potential altogether, I'm first going to wrap this use of
string compare by indenting this whole block of code and first only doing
that if s is not equal to null and t is not equal to null.
>> Wrapping that if else construct that I typed earlier with curly braces as
well, so that this time I only touch s and t if I'm certain that
they are not null.
Let's now save, compile, and re-run this program.
>> Make compare 1 dot slash compare 1.
I'll say hello again.
Followed by hello yet again.
And this time, I indeed type the same thing.