Hello, and welcome back. In this lecture, I want to start talking about string functions.
I mentioned earlier that strings,
character arrays are not
a variable data type in c.
So you can use any of the operators,
but you're still going to need to be able to do a lot of things on strings.
You're going to be able to manipulate them.
You're going to want to get the length. You want to compare them,
all sorts of things probably convert them.
So luckily the standard library comes with
a bunch of functions that you can use to perform these type of operations.
I'm going to first start talking about some of the most common ones
and then we'll move into some more specific ones in other lectures.
So we already know that a character
string is a char array terminated by a null character.
Character strings are commonly used in c programs.
C luckily provides many functions specifically designed to work with strings.
The ones that we're going to discuss are the most common ones that you'll use.
First, getting the length of a string using the str length function.
Copying one character string to another.
Remember, I told you earlier that you can't use the assignment operator to assign a string.
So what you do is you use str copy or strn copy.
Also, combining two character strings together, concatenation.
There's a str cat and a strn cat.
And determining if two character strings are equal.
Again, the equals equals operator is not going to work for comparing strings
because they are arrays.
So if you want to compare strings, you have to use special functions
provided by the c library. Str compare and strn compare.
First, let's talk about the length.
All these functions are defined in the string.header file,
so you have to include that when you call or invoke one of these functions.
Getting the length of a string.
Str length is a function that finds the length of the string, it's pretty straightforward.
It's returned as a size t variable.
You can pretty much use the size t variable interchangeably with an int.
But first, you'll have to include that string header file if you want to use str length
and this is how you use it.
You basically create a string literal inside double quotes and you assign it to a character array.
You don't specify the size of the character array.
The compiler will do that for you and will add the null terminator.
Once you have my string defined
with a string constant, you can then call str length and just pass in that character,
pretty nice.
It's one of the most often used ones because you often want to get this length of a string
when you do validation checks or anything really
if you're if you want to make sure there's no buffer overflows you can do that as well.
This function does change the string.
So the function header does not use constant
in declaring the formal parameter string.
If you were to go and look at string.h
inside user include in your directory,
you would see that it's not defined as a constant.
Copying strings. Copying strings are
either modifying an existing string,
modifying an existing character array that has some data in it.
Or if it doesn't have anything in it, you want to put something in there.
Since you cannot assign a raise in c, you cannot assign strings either. I mentioned this earlier,
declaring a character and then using the equals assignment operator does not work.
So what you have to do is you have to use the str copy function
to copy a string to an existing string.
It's the equivalent of an operator for strings, assignment operator.
If we declare two character arrays source and destination,
all we need to do is say str copy source and then
whatever the source is there we can put in the string
and the same with the destination.
You can also put variables in there as well if you wanted to copy those.
But what we're doing here is the first parameter is where you're copying to.
The second parameter is the string that you're copying.
So source would have this is source inside of it after str copy.
Destination or dest would have this is destination
inside of it after the str copy call.
The str copy function does not check to see whether the source string actually fits in the target string.
So what would happen if you had a big string.
So in this previous example what would happen if a destination had 100 characters in it.
And instead of having this as destination, it had 100 characters.
You tried copying it
into source that only had 50 characters. There would be a problem.
The destination string would be larger than the source string that you're copying.
So if we tried to copy str copy into source instead of copying this as source we put in
this is sourced five times or ten times whatever it is and one over 50,
there would be an error.
And I'll demonstrate that.
So a safer way to copying strings is to use strn copy. You'll notice that n
can be turned to strn copy.
Strn copy takes a third argument.
The maximum number of characters to copy.
So here you actually specify how much you want to copy.
So if you have a source of 40 and a destination of 12 sizes,
you can first do an m set which is just setting the size of the data inside a destination
to null terminators that way when you add something it will automatically be there, don't worry about that.
The str copy is going to copy hello how are you doing to source,
and it's not going to check if how how are you doing is larger than source.
But if you do a strn copy and you copy in a destination,
it's going to copy source and it's only going to copy the first 10 characters.
So that third argument should correspond to the size of the destination.
So let's go into code blocks and look at str length and look at copying strings to see
exactly how these work.
So here, we have our main.
And to get the string length string is really easy.
We're going to create our character array. We know how to create a character area. We don't want to specify the size,
but we got to put the name first and then the brackets.
And then we can initialize it with whatever we want,
say inside double quotes. Double quotes represent string literals.
So I can just say my name is Jason.
The end result of this is s
is going to be an array of size
17 characters.
All of these characters plus the null terminator.
The null terminator is the backslash zero.
You don't have to put that in there because it automatically gets added by the compiler.
That's how we can initialize and create a string in a character array.
Then to print it, all we have to do is say printf and say the length is.
And then we put the percent s
as the format specifier to display a string.
And then all we do is we say the name of the string.
And to not get confused let's just say my string.
And we don't have to use the ampersand like we did previously, previous examples,
we can just say my string. If we compile and run this,
we also may have to make sure we include the string header.
When we compile and run this, you're going to see the length of the string.
So the length is and I didn't call the string length function.
So you have to actually call the function if you want to display the length,
so you just do something like that. You put the string inside str length.
Now when you run it, you'd actually see the length of the string.
This has to not be a string, but an actual.
So that's how I would display the string. If I want to display the length of the string,
I do percent d in the call the function.
Now when I run it,
you'll notice that it actually prints out 16, which is the length.
So it's pretty easy function to use str length. If you want to
copy strings, you use str compare.
And you want to use strn compare to actually specify the bytes.
So here if we have my name is Jason,
and then we create another string,
actually we'll just keep it that. We won't even have to specify
and what we're going to do is we're going to say
create another string called temp
and that's going to be equal to str and compare,
we're going to pass in what we want to copy to.
Just create an empty character array,
and we're going to just make it a size 50.
And what we're going to do here is we're going to say strn copy.
The first parameter is where we want to copy do, we're going to say temp.
The second parameter is what we want to copy which is my string.
And then we don't want to pass in more than 50 bytes.
So we can actually say size of temp
to say we don't want to pass in more than 50 bytes. And you probably want to do -1
for the null terminator.
In this way, that third parameter is really important because we're never going to have buffer overflows.
You should always use strn copy and not str copy.
And then here we can just display our string to see if it was copied.
We could say temp, change it to a percent s.
And when we build this, we should see,
not the length, we should see the string is something like that
and end it with a semicolon.
Now we should compile we do.
So now we run it. We should just see our string that was copied in.
So we see my name is Jason
and that is stored inside of temp because we printed out temp.
But always use strn copy. Don't use str copy,
that will make sure that you don't have buffer overflows.
And the third parameter should always be
the destination, not the source,
the destination. It would be pointless to say copy this entire string.
Because if this is smaller, you're going to have problems. So always say the size of this.
And you see that you can use the sizeof operator
to see how many bytes are inside of character.
Let's talk about some more functions.
So we know how to get the length, we know how to copy strings, how about concatenating strings.
The str cap function takes two arguments, two strings as arguments
and it copies the second string at the end of the first.
The second string is not altered,
only the first.It'll return the value of its first argument.
The address of the first character the string to which the second string is appended.
So return something, telling you where you actually appended it if you ever needed to use that.
So here's an example, we create a character string of my string
and it just contains my string with a null terminator.
We create a character string of input, which is 80, it's empty.
We say enter a string to be concatenated. We read it in.
And then we concatenate it.
We print out the original string followed by the concatenated string and then the input.
So after this, whatever you entered would be put at the end
of my string. So if you enter Jason, it would be my space string Jason.
That's what my string would be equal to.
Because you do a str cat on it.
First we print out the string and then we call str cat on it.
Input is actually going to be equal to
whatever was input, my string followed by whatever was input.
We'll go through this example in a second. Now just like str
copy, str cat also has a strn end cat.
So the combined version becomes the new first string.
So just like string concatenation with the str
cat, there's a strn cat.
Str cat has the same problem with str copy. It can have a buffer overflow. It doesn't
actually make sure that the second string will fit in the first array.
So if you don't have enough space in that first array, you can run into buffer overflow problems.
So what you want to use a strn cat.
The second argument indicates the maximum number of characters to add.
So for example, if you do a strn cat
and two strings bugs an add-on, it's only going to add the first 13 characters of add-on to bugs,
depends which comes first. If add-on is less than 13,
then it will only add until you see the null terminator.
So in this example, we're creating two strings.
This is the source this destination. We're doing a strn cat.
We're only copying 15 characters into destination
at the end. So whatever the size of destination will be size of destination
plus 15 and then we print it out.
So let's look at this in code blocks again.
So here's our example and we're going to create
two character strings array, one size 50, the other size 50.
We're going to do two str copies to set them to data.
This is the source's destination.And then on line 12, we do the strn cat.
And destination is going to be what we concatenate to.
So the source is going to be what we're going to concatenate.
And we're only going to assign 15 characters.
So if source was bigger than 15,
we wouldn't copy all of them.
So when we build this and run it,
you're just going to see that it now says
the this is source file or it's going to say
this is destination followed by this is source.
So it says this is destination, this is source.
It concatenated that onto the end of destination.
So you always want to use strn cat for concatenation, so you don't have buffer overflows.
Let's talk about str compare, which I keep accidentally saying instead of
str copy, str compare.
So for comparing strings, we have to also use a function.
If you want to compare one string to another,
you can't use double equals, that only checks for references
whether or not you have the same address. So what you need to use is str compare.
This is going to compare the string contents.
It does not compare the array. So it can be used
to compare string stored in arrays of different sizes.
It does not compare characters.
You can use arguments such as apples and a in double quotes,
but you cannot use character arguments such as a single character a.
This function does for strings what relational operators do for numbers.
So the compare function
is going to return a value that tells you whether or not the string is equal,
whether it's larger or whether it's less than.
If it's equal, it's going to return zero.
If it's less than, it's going to return less than zero,
if str one is less than str two.
And if str two is less than str one it's going to return greater than zero.
And so what exactly does this mean? Well, let me provide you with an example
of how this works.
Let's put this in the code blocks and run it.
But here we're doing a bunch of str compares, and we're going to tell you what the values are,
whether they're less than or greater than because it's not very clear.
So let's take this and look at it in code blocks.
So here's our example. And we're comparing a bunch of strings and then just displaying
the results of str compare,
whether it's a zero one or whatever.
So if you compare two strings a and a, they have to be in double quotes.
And what you see here what we're doing is printing out what we're calling.
So we're delimiting
the quotes. We're putting the backslash so that we actually print out a quote character
and it doesn't end the string. But if we compare a and a,
this should return zero. Because zero means strings are equal.
If we compare a and b, str compare should return
less than.
Because a is less than b.
B a should return a greater than zero.
But let's go through this, make sure I don't make any mistakes
and we'll actually see it in the output.
So when we run this
and this helps you remember what numbers are going to be returned.
So when you compare a and a, you actually get 0.
If you compare a and b, b
is less than a, so it's a negative one,
something less than zero,I might have said the opposite,
see even I still get them confused. Because a is greater than b,
a is greater than b, negative one.
B is less than a, so you get something greater than zero which is a one.
C is greater than a, so you get one,
just when b was greater than a.
And then you're comparing uppercase and lowercase.
When you compare uppercase and lowercase, it does the inverse. Z is actually
less than lowercase a.
So capital is always less than lowercase.
If you try to compare apples and apple, it's going to be 1 because
apple is less than apples.
And that's how the comparisons works. It's important to understand the 0 greater
than 1 or less than 1. Again
less than zero is going to say that the first string is less than the
second string.
And greater than zero is going to say the first string is greater than the second string.
This function is very easy to use. You don't have to worry about
any kind of strn compare because it doesn't matter the size
of the arrays that you're comparing.
The str compare function compares strings until it finds corresponding characters that differ.
So it could be all the way to the end of one of the strings.
Strn compare function compares the strings until they differ or until it has
compared a number of characters specified by the third argument.
So in this case, you don't need to use strn compare
for buffer overflows. It exists, but you only would use it
if you only want to compare a certain number of characters.
It's used differently than
the str copy or the strn copy or the strn cat.
The strn compare just says I only want to display so many characters.
So it would be useful if you're only looking for a prefix.
So if you wanted to search for strings that begin with astro,
you could limit the search to the first five characters
and you would use strn compare this way.
So this example you see that we say equals equals zero.
You often see an if statement like this when you're using str compare.
You see str compare inside of this statement and equals equals zero to say that it's equal.
So strn compare uses the first five characters,
compares astro to the first five characters of astronomy.
It's going to say that it found it. It's going to be equal to zero, and it's going to say found astronomy.
If you do the same thing, but comparing it to a different string called astounding,
it's not going to find it, it's actually it's actually going to
display - it's going to say that astounding is greater than astro. So it's going to
display something greater than 0, which is a 1
and it's not going to display any data.
So use str compare when you're comparing strings for all the contents.
Use strn compare when you only want to compare prefix to a string.
Same type of syntax or strn compare just that third argument.
So we talked a lot about some really important
functions that we're going to use, mainly str length,
strn copy, strn cat, and strn compare.
We're going to have a challenge where you have to utilize these functions
that will get you familiar with it.
But you're going to become really familiar with this because you're going to end up
using them over and over and over again. Thank you.