- At this point, we're going to
advance our discussion on strings.
We'll look at some of the libraries
that the language C provides us for string manipulation.
There is a special external library
we haven't seen yet called string.h.
This library includes a series of functions
that are very essential in the string handling.
One of those functions is string compare.
It allows us to compare the value of two strings
and returns a number, which we will see shortly in the code,
that would indicate whether the strings are similar or not.
Another useful command is string copy,
which is useful to copy a string onto another.
Recall that if you want to copy
the value for variable into another,
you simply say, for example,
X equals Y and then the value of Y
will be copied onto X.
Since a string is not really a variable,
but a collection of multiple memory locations,
each one of them as a car,
we need to process strcpy a little bit different
because a string is not just a simple variable.
We are taking a source shortcut here, src,
taking all those positions that start at location source
and copying them onto another location,
starting at the label called destination or dest for short.
Another interesting function allows us to copy
a specific number of characters from source
to destination, not the entire one.
Finally, another useful function, strlen,
which simply gives us the number of characters in a string.
There are many, many commands that'll help you
manipulate strings in C.
Those are just four out of about two dozen,
which later on, you can look up the string.h library
and see which one is appropriate for your project at hand.
Let's see now how this works in code.
For the purpose of this assignment,
we have now defined two character,
string1 and string2.
Those are character type that define two arrays,
which we decide to call string1 and string2.
Let's look at a few scenarios.
SCENARIO 1, we are going to copy
the string "Anybody" and see to a variable called string1,
which is defined as an array of 20 positions,
each one being a car.
The second one, we'll copy "Anybody home" into string2.
So, we now have initialized two string variables
or arrays of characters,
one having the value "Anybody" on line 21,
the other one, "Anybody home", on line 22.
We will proceed now with calculating
the length over here on those strings.
Recall that strlen is the function defined
in the standard library as
the number of characters in a string.
If you were to count them,
"Anybody" has seven characters
and "Anybody" has slightly more.
The value computed by the built-in function, strlen,
is an int and it's being captured in a variable, len1.
Notice that I defined that variable, len1,
as being an int and assigned, right away, a value.
In some instances, we do not have to
define or declare the variable ahead of time.
We could just declare it when we need it.
Line 25 prints the length that has been returned by
strlen(string1) and we proceed
with doing the same with string2.
We have two more scenarios.
Notice that in SCENARIO 1,
string1 was shorter than string2.
SCENARIO 2, string1 is longer than string2
and in SCENARIO 3, the strings are identical.
We are, in each scenario, using
another function called string compare.
That's strcmp, for example, in line 52,
that compares string1 and string2.
Let's see how this works.
SCENARIO 1, string1 was shorter.
In case of a shorter string,
obviously, they are not equal.
The return value is -32.
When string compares returns a negative number,
a positive number or a zero,
those are the clues for us,
as the result of the comparison.
When the first string is shorter,
we get a negative result.
When the first string is longer,
we get a positive result.
When the strings are identical,
we get zero.
It is not enough for the strings to be identical in length.
They must be identical in their characters as well.
So, actually string compare provides us
with two pieces of information.
One, comparing the length of the string.
The other is comparing their content.
Those are just a few of the many, many
string manipulation functions,
which are available in C.
As we progress with our understanding of the C language
and all the powerful things it can do,
we will use an increasingly larger number of such string,
as we will see in other libraries
that serve different purposes.