In this video, I'd like to go over using the c++ string
class and some examples there.
So I'm in the section 10 workspace in the CPP strings project.
Now remember, the string class is a class.
So when we create strings in this case, here you see I'm creating
eight strings from S0 to S7 on lines 9 to 16, these are objects.
We'll talk about exactly what objects are in the world of object-oriented
programming in a future video.
But we are using object-oriented programming here to work with
these c++ string objects.
Okay, it's important that you include the string library.
Otherwise, you'll get a lot of compiler errors, and I'm
using the standard name space.
First thing is let's declare and initialize some
of these string objects.
Here, I'm declaring S0 through S7.
You can see S0 -- I'm just saying string S0.
It's important to understand that with classes and objects
in this case, S0 is an object.
I am getting an initialized object.
There's not going to be any garbage in there.
And we'll display that in a second just so you can
see what that looks like.
Then I'm declaring other string objects here.
So S1 is being initialized to apple.
S2 to banana.
S3 to kiwi.
S4 to apple and so forth.
S5 is being initialized to S1.
Well, S1 is apple.
So a copy of that string is going to be put into S5.
So S5 is going to be its own version of apple.
So if I change S1 it's not going to affect S5.
There's also a couple of other initializers.
There's actually a bunch of initializers and constructor
style initializers, a whole bunch of examples that you can use,
but I'm just sticking to the the commonly used ones in modern c++.
In this case, S6 is S1.
I'm initializing it from S1 which is apple, but I'm
only using a part of it.
I'm starting at index 0, and I want three characters.
So it's only going to be initialized to the string app.
And then in this case S7, I'm initializing that
string to 10 x characters.
Notice the x is in single quotes here, so that's a character.
And also notice that I'm using parentheses here, and not curlies.
This is a constructor style initialization, and that's the
one that works with this example.
Before we look at the initialization values, let's look at this right here.
Notice that I declared that string object S0.
If this had been what we the example that we saw using c style strings,
and we never initialized it you remember we saw that garbage,
you're not going to get that here.
So if I display S0, you won't see anything because it's empty.
And if I ask S0, what's your length, this is a method, this is an object
and this is what object-oriented programming is all about.
I'm asking the S0 object.
In other words, this string right here, hey, what's your length?
And it's going to return its length.
In this case, I expect back a 0 because it's an empty string.
So let me run this.
And you can see that nothing printed out because the string is empty.
And I get back a 0, which says that the string has no length, it's empty.
So you can see that it is being initialized.
That's really important with objects.
Okay, I'm going to comment that out, and then let's look at some
of these initialization examples.
I've just initialized those objects right up here where I declared them.
Now I'm just going to display them.
And you can see that we're getting exactly what we expected here, let
me scroll that up just a little bit.
So S1 is apple.
S2 is banana, then I've got kiwi.
Apple with a lowercase a, apple and app that's that three.
The initializer where we only wanted the first three characters.
And then I've got 10 xs.
Okay, so you can see really powerful, really easy way to
initialize these string objects.
So now let's comment this out again and work with some of the operators
and methods of the string class.
In this case, I'm comparing.
If you recall using c style strings, we had to use the strcmp or the string
compare function pass in two strings and get back either 0 or a positive
value or a negative value and then we'd have to check things like that.
Well, here we can just use the operators that we're used to.
And I'm using the boolalpha io manipulator here, just so it
displays the words true and false.
And you can see I've got a whole bunch of examples, and it's actually easier
to run it than to read all that code.
But you can see here that I'm comparing apple with
apple, and I get back true.
I'm comparing apple with banana, I get back false.
Then I'm asking is apple not equal to banana, that's true
because they're not equal.
Is apple less than banana, true because the capital a comes before
the capital b in the ascii table.
What about banana greater than apple, yes.
Again I just flipped the sign and flipped the strings.
What about apple with a lowercase a, is that less than
apple with an uppercase a?
It is not because the uppercase characters come before the lowercase
characters in the ascii table.
And then the last one I'm using is right here where I'm actually
comparing an object S1 with a c style string literal apple.
And that works perfectly fine because what happens is this string
literal right here gets converted to an object and then we're doing
object to object comparison.
So that's true.
Remember, it's doing character by character comparison.
Okay, so real familiar, real intuitive and very easy to use.
So let me comment that out, and let's do assignment.
Again, if you recall with the seesaw strings we had to do strcpy
or string copy, using c++ string objects, we just use the assignment
operator, just like we're used to.
So they feel like they're really part of the language, just like
you would with integers and doubles and characters and so forth.
So in this case, I'm assigning watermelon to S1.
Okay, whatever was in S1 before is now gone.
Okay, I've overwritten it with watermelon.
And then I'm outputting S1 is now.
And obviously, I expect back watermelon.
When I say assign S1 while S1 is watermelon to S2, I'm going to get a
copy of that watermelon object, right.
So they're not pointing to the same watermelon.
I'm going to get two copies that are individual.
So if I change S1, it won't affect S2 and vice versa.
And if I display S2, I also expect watermelon here.
Okay, here, I'm saying S3 is Frank.
So I'm assigning the string frank to S3 and that should also display Frank.
Now once I've assigned these strings, I can manipulate them.
So in this case, I can say change the zeroth character in S3.
Well, that's the F in Frank right here.
Change that to a capital C, and notice single quotes because
I'm changing a character.
So I'm saying assign the capital C to the first element of that string,
right, the one at the 0 index.
So this would change it to crank, which will print out crank.
I can also use the at method.
Again, this is the whole idea of object-oriented programming.
I'm saying, hey S3 change the character at
position 0 to a capital p.
So when I display that, it should display prank.
Let's run that and double check, so you can see here
that S1 is watermelon.
S2 is watermelon.
S3 is frank.
And then I'm changing the first letter of Frank to c and displaying Crank.
And then I'm changing it to a p and displaying Prank.
All right, I'll comment this, out and let's look at concatenation.
Again, I'll compare this to the c style string where we had to use
the string cat function strcat to concatenate two strings.
Here, we could just simply use the plus operator.
And in this case, I'm saying assign watermelon to S3.
Okay, that's easy.
But now I'm saying S3 equals S5.
Well, S5 is apple, and S2 is banana and juice.
I'm concatenating these 1, 2, 3, 4 strings together by
simply using the plus operator.
The plus operator is considered overloaded, that means it works with
integers, it works with doubles.
And in this case, it works with string objects.
So what we're going to do here is we're going to say apple and
banana juice and assign that to S3.
So in this case, watermelon is gone.
We're overriding that.
And then when we display S3, it should say apple and banana juice, right.
So let me run that quickly.
And there you go.
S3 is now apple and banana juice just like we expect.
If you try something like this, you're going to get a compiler error.
The problem here is that we've got a c style string literal
and another c style string literal and the plus operator.
C style strings don't work with the plus operator.
If you wanted to do something like this, you would have to scrcat
and concatenate them together.
In this case, you're going to get a compiler error saying
something about I don't know what to do with these two character
arrays and this binary operator.
Okay, so keep that in mind if you're trying to use the plus
operator, you've got to use it on at least one of the objects.
Notice over here, we're using an object and a c style string literal.
That's okay, because it'll convert.
But when you've got two c style string literals like this,
it doesn't know what to do.
Let's move on and talk about looping.
In this case, we're going to use a for loop to loop through a string.
Remember, a string is simply a collection, right, a bunch
of characters so we can easily loop through them.
And we'll loop through them two different ways.
First, we'll use a regular for loop, and then we'll use
the range-based for loop.
In this case, I'm assigning apple to S1, just so we have
something to start with right here.
And I'm initializing I to 0.
Notice that the type of I is size T.
This is really the proper way to do this.
It's always going to be a non-negative number, right because
we're looping through a string.
Now if you've got looping variables that need to go negative, you
don't want to use size T, you just want to use a regular integer
or something that's signed.
But in these kinds of examples, size T is the proper type to use.
So I'm initializing I to 0 and I'm going to loop while I is
less than the string's length.
Remember, we're going from 0 to the length minus one.
So in this case, I'm asking the S1 object, what's your length, and
it's returning 5 because it's apple.
So I'm going to loop 0, 1, 2, 3, 4 and then I'm going to stop.
And as you see I'm incrementing one each time.
So every time through the loop, all I'm doing is just simply outputting
that character at the ith location.
So cout S1.ati.
I could use array subscript syntax.
They both work.
The .at method does bounce checking, the subscript method does not.
So when I run this, I should see apple print out across the screen.
And there it is.
I'm just displaying it one letter at a time.
I can also do this using the range-based for loop in which
case I'm just saying for every character cin S1 output it, right.
So it's going to go a p p l e, stop.
So in this case, I should have a c apple twice now, right.
Once for the for loop and once for the range based for loop.
You can see some of these are very, very intuitive and easy to use.
There's a couple left.
Let's talk about substring here.
This was, again, is very intuitive.
Suppose S1 is -- this is a test.
I've just assigned that string to S1.
And I want the substring that starts at 0 and it's 4 long.
So it's going to go t h i s, right.
The capital t is index 0, so that's going to return the
word this or the string this.
In the second substring example, I'm starting at
index 5, which is 0 1 2 3 4 5.
So it's starting at the I and is.
And I want two characters, so it's grabbing this is, right there.
And that's what it's returning.
And then the last one, I'm starting at index 10, which is the lowercase
t and test, and I just want 4 back, and it's giving me test.
So you can see, it's very, very easy.
And of course all these you can see here it's displaying this is a test.
All these are hard coded but you can certainly use variables for these
integers and loop through and loops and grab pieces of substrings, and
you can do a lot of really cool stuff.
We can erase characters.
In this example, I'm saying S1.
Again, I'm assigning this is a test to it.
And I'm erasing from S1.
Again, notice the object-oriented style.
Think of this almost as you're asking
the S1 object, right because notice it's S1.
So you're saying the erase method is going to execute on that object S1
because we could have S2.erase S8.erase, anything.erase, right.
But in this case, it's happening on S1, which is
this is a test right here.
So what I'm saying here is erase starting at 0 and erase 5 characters.
So it's going to erase the capital t, the h, the i, the s and the space.
When I display that, S1 is now just the is a test part.
I got rid of the this and the space from the beginning.
All right, let's do a couple of more.
The getline is a little bit different from the getline
that we saw with c strings.
If you remember, with c strings we said cin.getline, and we had a length.
Well, we're not worried about a length here because the c++ string
class handles lengths for us.
So it looks a little different, but it's the same basic effect.
Here, I've got a new string that I'm declaring called full name
and I'm initializing it to empty.
I'm asking the user to enter their full name, and then I'm
using the getline function here.
And I'm passing in where I want to get the input from, in
this case the keyboard or cin.
And where I want to store the result, full name.
This will get an entire line up until I press enter, and
then I just display the name.
So in this case, if I run this and type my name,
notice full name is
Frank Metropolous, my entire name, it doesn't stop at the space the same
way that the extraction operator does.
The last thing we'll do is we'll look at the find method,
which is a real handy method.
So in this case, here's the secret word.
The secret word is boo, that's the string.
And I've got a word variable here, which is a string object.
I'm asking the user to enter the word to find.
The idea is they're going to type one of these words in here, right.
And I'm going to tell them where that word is in the string,
what position it begins at.
So if I typed in the, I'm saying hey S1.
Again, the S1 object, find that word within you.
So it's going to find the word the within it, and it's going
to return 0 because it starts at the 0 index, right there.
That's the t.
Again, I'm using a size t for the type because it's always going to
be like 0 or greater or an error.
And I'll talk about the error in a second.
So at this point, I'm looking for that word in the string
and position holds its position whether it was found or not.
How do I know whether it was found or not, I need to see if the position
returned was that value, right, string end position, no position.
So in this case, if the position I got back is not
equal to that, then I found it.
So I'm simply going to disability found word at position whatever.
Otherwise, it wasn't found.
So let's run this.
And remember, the string is the secret word is boo.
So I'm going to run this and and search for boo.
And it says I found it at position 19.
Let's run it again, and we'll use the word secret this time.
It found it at position 4, and you can see there it is,
right there at position 4.
And we could try to find something that's not there like hello.
And it says hello not found.
Okay, so let me wrap up this video now.
You have all the elements you need for the challenge that's coming up next.
You could certainly try the challenge using c style
strings and then c++ strings.
But I'm just going to do it using c++ strings because we really should
be using c++ strings going forward.
All right, So head off to the challenge.