OK, so this is a follow up lecture about strings, that first lecture was just a really easy introduction
into what a string is.
And I showed you a little bit of stuff that you could do with it, like subscript dean index indexing
to like, print out a character in a certain position like the H.
You know what position zero or the position one or something like that.
We did the whole hello, I remember, but then I showed you the length as well, right?
Escort length.
We use that length function, and I said I would mention a little bit more about other functions and
stuff like that.
I have decided that I'm actually not going to drop a bunch of functions on you.
I think that it would just kind of confuse you and wouldn't really help as much at this point, since
we're trying to just learn how to program and be better programmers now.
Think later on you could go look at the extensive documentation of these strings, and I will point
you towards that later on so you can use some more advanced string functions.
But the thing is is we're trying to just learn how to mess with strings.
We will encounter them in some of the problems that we will be solving very soon.
And so I think what's better is to just introduce only two more things at this point in time about strings,
which are actually instead of functions going to be more associated with the operators.
So that is what we are going to be doing.
So the first thing that I want to cover is about digging a little bit more into the subscript operator.
So remember that we did something like s sub zero just indexing the position zero.
And we actually, you know, did a C out with that and we were able to print this out and it was an
h right because position zero was h and then one, two, three, four or five and so on, right?
So that was cool and all.
But that's not the only thing you can do with this.
Something pretty cool is that if you want, you can actually change.
Some of the individual characters in here, so let's say I wanted to change the E and Hello world to
an A..
I can actually do that by using a combination of this subscript operator and using the assignment operator
like this.
And on the right side of the assignment operator, I can actually put whatever character I want to be
written to that position.
So if I want to write an A., This would overwrite the H in position zero, but I can change this to
one.
And this would overwrite the E.
So what this statement is saying here is, OK, we're looking at the string s, which is a variable
rate.
It's a variable type string.
It contains hello world.
What does it position won in that?
OK.
We're using these brackets, so that means position one, let's go there and check it out.
This is zero.
This is one.
OK, position one.
There's an E.
What are we doing with this position?
Well, we're using the assignment operator.
So what we're going to do is say, take this character note the single quotes around it.
This is a character.
That's why they're single quotes.
We're going to take this character a we're going to assign it to this position.
One in the string hello world.
So overwrite the E with an A..
So let's actually confirmed that's what happens.
I'm going to go over and copy this and paste it here so we can see a before and after Line five is basically
the before and seven is that after we change to a.
So let's go ahead and let's
clear the screen and I'm going to compile this.
And so you notice that I'm doing the G Plus Plus doing so secret Peter Erskine stuff out.
You can call it whatever you want.
And now I'm running it.
And we noticed that it actually gets changed.
So it said Hello world, and the first one and the second one says, Hello world.
With that, a so pretty cool, right?
But there is more.
So before I go onto this next thing, I want to go over, though I want to kind of let us have a chance
to use some of the things we have learned in conjunction with these new things.
So what I want you to do is pretend that you don't know how long this string is and you might be saying,
Well, why would I not know how long it is?
It's right here in front of me.
It's hello world.
It's blatantly length.
11, you know, one two three four five six seven eight nine, 10 11.
I know how long it is.
Well, what's going to happen in the near future in this course is that we are going to stop relying
on writing our own data in the files, and we are actually going to be taking in data from the user
where the user enters the input on the keyboard.
So what will happen is the we it it doesn't necessarily have to be another user, it'll be us testing
it out.
But we will run the program and the program will wait for us to type stuff on the keyboard.
And that stuff we type will be the data that goes into our program.
So rather than this hello world right here, it might be us typing it on the keyboard when the program
runs instead of typing it straight into the file right now while we're making the program.
So if you do that, you won't really know how long the input is, I can type like a super long sentence
or something, and if I want to create a string in the program that has that thing that was typed,
then I will be not knowing that its length 11, you know, it might be length like 20 or length 100
length a thousand.
So with that in mind, let's just pretend that that is what's going on.
We haven't talked about how to get input from the user yet, but that's coming up really soon.
So let's just pretend right now, though, that we don't know how long this string, as is.
We don't know what it contains.
We don't know how long it is.
We just know that the string is s and it has a string in there.
How would you change the last character of the string to the letter A.?
So in this case, it will be D..
But what I'm saying is that you're not allowed to do something like this.
You can't be like, you know, as of 10, because that would be a position 10, right, you cannot do
as of 10 equals a.
So not allowed to do that because you have to pretend like you don't know how long it is.
How are you going to figure that out?
What are you going to use?
What did we just learn and think about what we just learned in the last lecture and how you could do
this?
I think about things that we learned to lectures and got like the lecture before last lecture, so I'm
I'm going to kind of implore you to pause the video and see if you can figure this out on your own.
Otherwise, I'm going to solve it for you right now.
So if you can pause the video and see if you can do this.
OK, so I am actually going to go over the solution now and show you a couple of different versions
of it.
So let's think about what the statement of this problem was a very simple problem.
We'll be getting into harder ones later, of course.
So what I said was, I want you to change the last character of the string as pretending it could be
any string, any length.
In this case, it's hello world, but we're trying to pretend that we don't know it's hello.
I want you to change the last character to the letter A..
So one thing that we can use, and I'm going to comment this out right now.
One thing that we learned was.
How to get the length right so we can get the length with escort length.
So start length, what would that give us?
We think about it, it gives us 11, right, because this is 11 one two three four five six seven eight
nine 10 11.
Notice that I started counting at one, though.
When we look at positions in the string, we don't start counting at one, do we start counting at zero,
so zero one two three four five six seven eight nine 10 and that's kind of a little heartless.
I said, you know, you cannot go like s of 10 equals a.
So we know that the last character, D, is actually at 10, but we can't hard code that we know the
length is 11 and it's 11 because we start counting at one.
When we're doing the length rather than counting, it is zero for the position.
What this means is that the last character in a string is always going to be one less than the length
because if you start counting at one instead of zero, that is just saying the same thing as you're
basically adding one to each position.
So if this is a position 10, you add one to it.
Basically, you know, for it to be the length you go backwards for the other direction.
So the last position is at length minus one.
So that length minus one.
So something like this, what I want to do is I want to save this to a variable, though, because I
want to be able to use it.
So I'm going to make an integer because start length is a number, right?
We saw before that, it gave us 11.
So I'm going to say string length and string length equals escalates.
That gives me the length.
But I wanted minus one, right?
So what I could do is say, and string length equals escort length minus one.
Yeah, I can do that.
Pretty cool, right?
I can actually use more than just start length and on the same line, I can use some math here if this
is kind of seeming like a little too much for you.
You don't have to put this on the same line if you like.
You can just say string length equals escort length.
And that kind of makes sense, right?
Because we call it string length, we didn't call it last position or something.
So this is the length of the string.
If we wanted to do that length minus one, it might be more appropriate to name this variable like last
position or something like that.
I'm going to leave it as it's I'm going to leave it as just a start length.
So what do we need to do now, though?
We need to subtract from it so we can do this a few ways and you should know how to do this.
How do we subtract one from a variable?
So you notice that we did something like i1 equals i1 minus one?
That's something we did, but you notice that you're actually able to replace that with the minus equals
so we can actually do string length minus equals one.
You could also, if you want, you could do it the long way.
Right, we've talked about string length on this one.
But what's something else we can do notice we're only doing minus one right here, so we can even make
a shorter version of this, that shorter than both of those?
And that is just minus minus string length.
I remember that that is just the prefix decrement operator.
So we don't want to do.
We're not going to do a string length minus minus.
We're just going to do minus minus string like to be sure that it's happening right now as we should
do it with the prefix rather than the postfix, so that will subtract one from it.
And now what?
So now we have string length actually equaling something that is at the right position.
It's at position 10, you know?
But more importantly, it's at the last position of whatever as is because we know that if the length
was greater than it would still work and I will show you that as well.
So now that we have the position, all we have to do is use this right here.
We just need to do this line.
We need to like index it, but we're not going to index it at one, right?
We're going to index it at the string length because string length is a variable that holds the correct
number.
So might seem a little confusing, but you don't have to put a number in here like a hard coded number.
This is basically a number now, right, because it was 11 right here after we did this line.
It was 11, for this example, and then we did a minus minus, and now it holds 10.
So essentially something like this as 10 equals a which I told you not to do that is the same thing
as doing this.
So pretty cool, right?
This is basically this string length.
You can imagine it being a number 10 because that's what it stores inside of it.
From this line right here, that was 11, it turns to 10.
Now we use the number 10, but trapped in this variable here, which could be anything, right?
If this was a bigger string, this might not be 10.
It could be any number, but we know that it's the right position.
So pretty cool that we can do that.
Let's go ahead and put some see out statements so we can actually confirm what's going on here.
So what I'm going to do is I'm going to print out the string length.
And in the line, and you know what I might do, this is a minus minus string length, but
I'm just maybe going to call this.
Last position just because here when I minus minus, it really becomes the last position, right?
I think that's a better name for it since we're doing a minus minus here, right here.
It could be the string length in what might be better to do last position minus one, but I want you
to be able to use your minus minus separator and stuff like that.
The decrement operator and and separately use the length and kind of make it nice for you.
You can, of course, do it all on one line and you can just do that.
So I'm going to call in that last position, so I'm going to change this as well.
So it makes more sense.
We print out now this is the last position.
And then what we're going to do is also here.
Print this out.
So I'm actually going to copy this and paste it here.
And I just did that, you know, not really going to a huge room tutorial, but what you can do is go
to the front of this if you want to copy paste big chunks of code.
I pressed V, you notice that it says visual down here.
So this is visual block mode.
And then when you press W., it goes to the end of a word, so you can just press W a few times and
you notice it's like jumping to the end of each one of these words.
The cursor, although this is not highlighted here in the semicolon, since the cursor is under it,
it's going to capture this as well.
When we copy it, to copy it, you press y, which stands for a yank.
So I press that and it yanks it.
Then I'm I'm like, you know, making a new line.
But now I'm in escape mode.
You know, it's command mode.
I can move around and I press p in that piece that I'm going to undo that, though, because I don't
want to do that.
I already did it.
So we're going to print out the last position, which you know right here should be 11.
So we'll print out 11 and then we do a minus minus, so it becomes 10 on this line.
Then we print it out, so it should say 10.
Then we change whatever position 10, which should be the D and then we print that out.
So let's go ahead and test this out and we go ahead and compile it and run it.
OK, so pretty cool, we had Hello, World.
This is the last position when it was 11 and last position when it was 10, I'm actually going to put
something here just so we know that that's what it is.
So last position equals and I'll put that here to settle last position equals and then now we can compile
it and run again.
And now you notice his last position equals 11.
Then we did a minus minus, the last position equals 10, and then we change whatever in position 10
or change.
When I was at the last position, which was the D two and A and we see our result here.
So pretty cool, right?
We're learning how to do a lot more stuff, which, you know, this is really interesting.
So you know, this is a pretty powerful thing that you can change this.
Another thing that you might want to do with the length is be able to kind of check things like you
can use the, you know, an if statement.
So we could combine some more stuff with this.
And let's just kind of add another little condition to our program.
Let's say that if it's a D, we don't want to if it's not a D, if the last position is not a character
D, then we will change it to an A..
But if it is a D, we will change it to an X.
So let's go ahead and see if we can do that.
That way, we can combine our knowledge of conditional statements to to handle this.
So I'm going to put an f here, but let's see if you can do this on your own.
So what I am giving you is another really small challenge.
Now you kind of know how to change the character.
What I want you to do is use f and else, and I want you to change the last character of the string
s to an A..
If the last character is currently a D, otherwise or else, I want you to change the last character
to an X. So go ahead and pause the video and see if you can do that.
OK, so I'm going to go ahead and tell you how to do this if you haven't gotten it already.
So what we're going to do is say, all right, so if the last position of the string so s index last
position, if that is equal, equal to right, that's how we check a quality d, then.
What we're going to do is put this last position equals a.
So I'm actually going to paste that here, and that didn't work out well with my paste,
and we'll
clean that up a bit.
So if that's the case, if it is a D, so I use this equal equal.
Remember, this is the equality operator, then I want to change it to an a.
Otherwise, I'm going to say else and then I'm going to do as a last position is equal to X kind of
pointless.
I mean, this isn't really doing it doesn't really make sense, of course.
Why would we change it to hello world or hello world or something like that?
But nevertheless, it kind of exercises the things that we've learned, right?
So congratulations if you're going to get this.
If you weren't able to.
Don't worry, it will come soon enough and just need some more practice.
So the more you drill this, the better you'll get, the more you can combine.
The things that you've learned so far, the better you will be.
So practice, practice, practice and I will come up with more problems in the very near future.
We're going to go over a lot of different problems.
I'll kind of set out for you to learn this stuff better, but we just need to add a few more things
to our toolbox, if you know what I mean.
So pretty cool.
Now we figured out how to use conditionals and change these things based on the conditions.
So that's kind of all I have to say about the subscript operator being used with strings.
Also, hopefully you understand the fact that this is very similar to an array of characters.
It's just like a character here, character here.
Like it has all these positions, just like an array technically in the C language you don't have access
to and data type called a string.
So you have to make your own strings, which are called C style strings.
You can actually use those C strings in C++ as well.
And those are like literal arrays of characters the way that the string data type does.
This array of characters is kind of like that.
It is an array of characters.
I think it does like a constant array of characters, but we're kind of not focusing on that right now.
Later on, I think we will get more into the internal representation in how to use C strings.
It is more difficult.
You have to do more work on your own and that has to do a little bit with like pointers in memory management,
which are other topics that we're not yet getting into.
So that's why I don't want to mention it right now, because I feel like it might be a little overkill.
What I want to do is just introduce you to this string.
Data type and show you some cool things that you can use.
Like I said, there's a lot more of this dart stuff, you know, that length is just one of many functions,
many member functions you can use with the strings.
So later on, we will take a deeper look at those, but right now, I don't want to overload you with
like 20 different member functions that you won't even necessarily need to use in the upcoming session.
We want to focus on using these more primitive tools that we have, like the subscript operator and
things like that to manipulate strings.
So that's why we're focusing on it in this way.
That's why I am kind of not showing you how deep the string rabbit hole goes, but you are welcome to
explore it on your own.
And later on, I will be going deeper into all the stuff you can use with strings.
All of the methods, the functions and how to use the rest of the operators, as well as how to use
C style strings from the C programming language.
OK, so I felt like it was important that I at least mention that.
But let's bring our focus back to just what we've learned here with this subscript operator.
We now know that we can print stuff out with this.
We can also use it to change things inside of our string.
We know that we can use the length and subtract one to be able to get the last position as well.
So pretty cool, we also used some conditionals here.
So now I'm going to go ahead and move on to the other thing that I want to show you about strings,
and it also uses an operator.
And what it does.
I'm going to go ahead and actually remove all of this stuff right here because you don't need that anymore.
So this next thing is what we can use to add two strings together, when I say add, I really mean squish
them together.
So let's say that you didn't just want hello world.
Let's say you suddenly are like, Well, I want it to not just say hello world, but I want to change
it to say hello world again or something like that.
So this is something you can do using an operator that you've already learned, and that is going to
be the plus equal operator.
So you might be thinking, that's weird, because when I used plus equal Alice using it to add stuff
to an already existing thing kind of like that I one plus equals five.
You know, that added five to I one.
So the thing is, is that when you're using it with strings, it's more like tacking on things to the
end.
So if I had like, you know, some string that was like, you know?
T h and I wanted to tack on E to it like another string to make the word the.
Then I could do that by using the plus equal operator similar to how we did with the numbers.
Yet it's going to just take this E..
And when you're thinking about the plus as addition, the adding really means add it onto the end of
whatever exists already.
So you're going to do it something like this, so you would be like X Plus equals, and I could just
say like this like s.
So now I changed it to Hello world.
So let's go ahead and see what this looks like.
I'm actually going to clear before I run this.
Of course, so you notice that what it did is it took that s and it just tacked it right on to the end.
This is something called concatenation or a pindi.
So you can think of it as both of those things.
Interestingly enough, the string, a data type, has one of those dot functions, just like dot length,
which appends to the end of it as well.
But I feel like right now I just I'm going to introduce the operator and focus on us using that because
we've already discussed the +Y cooperator and it's good that we can use it.
So if I wanted to say hello world again, I could do the same thing and just put a space.
And then then again, why the space?
Because if I don't put this space, it just smashes it right up against the end of it, just like the
SW.
So you notice, if I do that, I compile it.
I run.
It is like, Hello world again.
You know, I want that space in there and the space is recognized.
It's not like it's not recognized.
So if I do this, I compile it.
I run it again.
Now we have a nice space between the weird world and the word again.
So also a really cool thing, so this is basically all I'm going to go over at this point with strings,
I kind of mentioned a few things that we can get into later about strings.
I like to mention these things that we haven't done quite yet.
Just in case some of you are like, OK, you know, let's get a move on.
I actually know about strings a little bit, but I want to, like, go deep with them and no more.
I'm just kind of like pointing that out if you really want to jump ahead.
The thing is, I don't recommend it if you're starting out beginning programming from scratch with this
course, because the main important focus right now is us learning how to become good programmers and
utilizing the C++ language to do that.
We don't need to be power users of C++ yet.
You don't need to be someone who's like answering Stack Overflow questions for C++.
We're just using it as a tool to help us become competent programmers.
You don't need to know the entire string library, all the member functions about constructors and C
strings and the array of characters and how those are organized quite yet.
We want to know about strings because knowing this basic amount that we've just talked about gives us
the ability to create interesting problems to solve and the more problems that we solve.
The better programmers we become.
These string problems are great for us figuring out how to manipulate, modify, take in and do things
with data and the fact that they behave kind of like arrays with these positions like zero one, two
three and so on makes us able to do some interesting things with them.
And since we're working with a medium that is an English spoken language or whatever language, it doesn't
have a character set of other languages that, you know, like Russian or Chinese or anything in this
course, of course, but it's nice for us to look at something familiar.
If you are familiar with the English language, hopefully the way this course would be hard for you,
then we can use this thing that we're familiar with to come up with interesting problems, and it will
help us wrap our minds around how to go about solving these problems.
These problems will relate to a lot of stuff and pure science, so that is why I introduce just a little
bit on strings.
So we can just take this a little bit and we can use this a little bit to create our own solutions,
rather than just using some type of function that does everything for us right now.
Kind of a long winded explanation for why I'm doing this, but I wanted to say that to put that out
there, in case you're wondering why we're not going off the deep end down the rabbit hole into the
depths of what a string is right now.
Just think that strings have a few important things you need to put double quotes around them, that's
what makes it a string, as well as putting these string data type.
It also needs to have this study, Colin Cullen, unless you are using namespace up here like this,
but and then as well, you know, you may or may not need to include this at certain points.
But if you're going to use something like this, like escort length, you definitely need to include
string.
But depending on your version of C++, you might be able to get away with making strings and printing
them out with just the i o stream being included right here.
OK, so with that, I'm going to let you go and not talk about the strings anymore.
We will be using them, though, in the near future when we learn about loops and taking in user input.
So with that, I will see you in the next lectures.