In this video, we'll go over some examples of using c-style strings.
First thing you'll notice is I'm including cstring.
I need to include that header file for the c-style string functions.
And I'm also including cctype that's because I'm going to
use some of the character-based functions in this example.
Okay, so I'm using namespace standard, and I've declared four
c-style string variables, which are really just arrays of characters.
On lines 9 through 12, first name, last name, full name and temp.
And I've made them to be pretty big: 20, 20, 50 and 50.
That should allow us to put stuff in there without exceeding the bounds.
Okay, so the first thing we'll do is notice here
first name is uninitialized.
They're actually all uninitialized.
Let's display first name.
This is a real problem because we've never initialized that array.
So it's very likely we're going to get garbage.
So let's run this.
And by the way, I'm in the section 10 workspace in the
c-style strings project.
So let me run this.
And there you can see the garbage right up here
and see it right in there.
What displays is who knows, right.
Whatever characters happen to be in those locations.
You might get lucky, and the null character might be in
the first location in which case nothing will print.
But more often than not you'll get garbage like this.
And this is a real common sin.
And whether you're displaying to the console or inside a window in
Microsoft windows or x window, and you see something that displays like that
on a button or up here on a title bar or something, that's a classic
symptom of uninitialized string.
Let me comment that out.
We don't want uninitialized strings.
Now obviously, we'd want to initialize these guys just like
that or to a string if we want.
So let me just go ahead and initialize all of them, which
is really the best practice.
Okay, so now they're all initialized.
So the first thing we'll do is I'll uncomment these lines here.
And this is nothing
we haven't seen before. Pretty straightforward, right.
Enter your first name and we're using the extraction operator to read
our first name into that variable first name, which is the array.
So whatever I type in, I'm just going to use my name in these examples.
Frank will be stored into first name.
It'll be f-r-a-n-k, and then the null character will automatically
be put in there for us.
Same thing with last name.
Okay, so again, nothing we haven't seen before.
Now here, let's talk about these two lines.
In this case, let me grab my marker real quick.
In this case, we're saying hello first name.
Now this will be replaced with the value of first name.
What's the value of first name.
Well, at this point i have Frank in there.
And remember, this is 20 big here.
But in this case, let's just say I've got that that's the null character.
So how do we determine the value of first name.
Well, we start here.
Do we see a null, no.
We move over.
Do we see a null, no.
And we keep going until we see that null.
At that point, that's what's returned.
So this will display Frank.
That's really what's going on when we're displaying
these c-style variables.
So it's going to say hello Frank your first name has.
And then we see this function right here the strlen or
the string length function.
A function is nothing more than a request for somebody
else to do something for you.
And we'll write functions really soon i think in the next section.
So in this case, I'm calling the strlen function I'm passing in
that array of characters Frank.
So what's it going to do? It's going to do the same thing
we just did here, right.
Let me clean this up a little bit, and we'll walk through this again.
So in this case, how does the string length determine
the length of that name.
Well, you start here.
Is that a null, no.
That's one.
Is that a null, no.
That's two.
How about this guy, nope.
That's three.
The n, nope.
The k, that's five.
Now we see the null.
The length is five.
That's what's being returned here.
Now strlen doesn't return an integer.
It doesn't really return an unsigned integer.
It returns a type called size t.
And I haven't talked about that yet but I'm going to now.
Size t really is an unsigned integer.
It could be an unsigned long.
You really don't know what it is based on your system,
that's the whole point.
We really should be using size t because regardless of what
system you're on, this will work.
You can be sure it will be unsigned though, right.
Because it doesn't make sense to have negative sizes for strings or
negative sizes for loop variables and things like that typically.
So from this point forward, I'm going to start using
size t where appropriate rather than enter unsigned.
It's the more correct way of doing it.
Okay, so that'll return 5.
So this will display hello Frank your first name has five characters.
And your last name, same idea.
It'll print out my last name here.
And my last name has 11 characters, so it should display 11 characters.
Okay, so let's run this example.
My first name is Frank, and I'll put in my last name Mitropoulos.
And it says hello Frank your first name has five characters and
your last name Mitropoulos has 11 characters, exactly what we expect.
Now everything I'm talking about in this video is totally
dependent on that guy right there.
If for whatever reason those null characters or that null
character is not there, you can imagine, right, we're going to
keep counting until we hit one which way maybe you know a 1000
characters down the line in memory.
Typically, you don't have to worry about it when you use
these functions, strlen, cin, strcat, like we see down here.
They handle that for you.
But if you're working with strings on your own and you're
processing characters and moving characters around, you've got
to be aware that you need to null terminate your strings.
Otherwise, you're going to run into problems.
All right, let me clear this up real quick.
And let's do another example down here.
I'll uncomment this piece of code out and we'll talk about that real quick.
So what do we have now.
Well, we have Frank in first name, right.
So first name is Frank.
I just typed it in.
And I'll type it in again in the next example.
What we're doing here is we're copying using the string copy function.
We're copying first name to full name.
This is the way it works.
It's going from this right argument to the left one,
not the other way around.
This is confusing sometimes for students.
So I'm copying first name into full name.
So again, here was first name.
And it had Frank in it.
And over here is full name.
Again, this is first name, and this is full name.
How does it copy.
Well, one character at a time it's going to copy the
F the R the A the N the K.
And it's going to keep copying until it hits that null character.
At that point, it copies the null character and it stops.
Okay, so at this point full name will have Frank followed
by the null character.
The strcat function is the concatenate function.
In this case what I'm doing is I'm adding a space.
You can see the space right there in double quotes.
I'm adding the space to full name.
So now it's putting a space here, right in there.
And then it's putting the null character.
So at this point, that's going to be Frank followed by a space.
I'll just use that underscore to denote the space.
Then I'm doing another string cat.
And what I'm doing now is I'm concatenating.
Again, this goes this way.
I'm concatenating the last name to a full name.
So now I'm putting in my last name here.
And it will also put in the null character at the end.
And then I'm just displaying it.
Okay, so notice what we're doing here.
What we're doing is we're basically building up full name
from first name and last name using strcat or string cat.
Let's run this.
And what I'll do here is I'll say again my first name is Frank
and my last name is Mitropoulos.
Hello Frank all that's the same.
And then it says here your full name is Frank Mitropoulos.
Notice how it concatenated the Frank the space and the Mitropoulos into one.
So full name now contains that string.
Okay, so let's do another example.
I'm going to comment all of this out so we're fresh every time.
And in this example, here this is an example that shows you
one of the behaviors of the cin extraction operator here.
What I'm doing here is I'm prompting the user me this case
for my full name and I'm going to enter the full name and I'm
going to display the full name.
Okay, so let me run that.
And I'm just going to say my full name is Frank Mitropoulos.
But now when I try to display my full name, notice I only get Frank.
That's the normal behavior for that extraction operator.
It's read again f-r-a-n-k space that's a whitespace, so it stops.
That's it.
Okay, sometimes you want to be able to read the entire
name, right, the whole line.
So this is what we'll do in these next few lines here.
In this case, again, I'm prompting enter your full name.
But now I'm using cin's getline function.
This works again with c-style strings.
So in this case, I'm providing the name of that c-style
string, that array name here is full name and a limit.
This is how many characters maximum you're going to read.
So I'm saying cin getline.
So this will stop either at 50 if you reach the max or
when the user presses enter.
So in this case, it's going to read the whole name, right.
So let me run that again.
And I'll say Frank Mitropoulos again and press enter. You can
see that the full name now is the first name and the last name.
So it's reading the entire line.
Now let me do a couple of more examples.
Let's do a comparison example here.
In this case, remember, full name will have my full
name in it Frank Mitropoulos.
And when I'm doing that I'm copying that to temp.
Temp if you recall, I'll scroll up real quick.
Temp is another array of characters, pretty big and that's 50 size.
So my name will definitely fit in there.
And when I say big size, my name will fit in there.
That's one of the things you always have to worry about when you're
working with c-style strings.
You've always got to be sure that you've got enough storage.
If you don't have enough storage, you're going to have a problem
because you're going to run over the bounds and probably clobber some
other memory location with something.
Okay, so again, let's assume that I've typed in my name and now I'm
going to copy my name to temp.
Then I want to compare those two.
They should be the same, right because I just copied
it to another variable name.
Well, we can use the string compare function.
We pass in the two strings that we want to compare, in
this case, temp and full name.
And if we get back a 0 that means that they're the same.
If we don't get back a 0, that means they're not the same.
So in this example, I expect to get back a 0.
So let me run this real quick, and I'll type in my full name.
They are the same Frank Mitropoulos and Frank Mitropoulos are the same
because it's the same string.
So I did get back to 0.
Okay, so now let's modify that string and then we'll compare it again.
So what we'll do this time we'll use a for loop, and I'll
uncomment this code out real quick.
And you'll notice what we're doing we're going from I equals 0 and notice
I'm using size t here instead of into or unsigned, this is the better way to
do it, the more correct way to do it.
So I'm going from I equals 0, while I is less than the
string length of the full name.
Again, string length is going to return however many
characters are in there.
I want to be sure I'm going less than that because remember we're
going from 0 to the size minus one.
And I'm incrementing I by one each time.
Then I'm using one of the character functions.
I'm saying full name sub I.
That's the specific character, right.
So it's going to be f r a n k and so forth.
If it's an alpha character, right, a letter.
Then I'm going to convert that letter to uppercase and put
it right back where it was.
So now my name will be all capitalized.
Okay, so let's run that.
And I'll type in my name with mixed upper and lowercase.
You can see here that it compared them and they're the same.
And now my name is all uppercase because I used the two upper function.
So now let's compare those two now.
Obviously, they're not the same, right because I just made
one a little bit different.
So let's do this.
Let's comment all of this out and walk through it.
And so what's going on here.
I'm comparing temp and full name.
They're not the same anymore, right.
Full name is now all uppercase and temp is not.
So I'm going to compare them, and it's going to say are they
the same are they different.
Now that I expect this to be different because this will not be equal to 0.
So what - how does this work with the 0.
Well, if the two strings are the same and that's character by
character lexically, the string compare function will return a 0.
Otherwise, it'll return a negative number or a positive
number depending on which of the string is larger, right.
So in this case, I'm comparing them temp and full name and then full name
and temp so you can see the result.
So let's run this.
So again, I'll use my name and press enter.
So as you can see, the two strings are now different.
And the string compare function will return 0 if
the two strings are the same.
You can see here that we're comparing two different strings.
It returns less than 0 if the first string lexically comes before the
second string or return greater than 0 if the first string lexically
comes after the second string.
Okay, so that completes this video.
This gives you a little bit of insight as how to use c-style
strings and their functions.
What we'll do next is we'll talk about c++ strings.