OK.
Welcome back to another lecture.
In today's lecture, we will be going over something called strings, which I have kind of already introduced
to you.
If you think back to things like Hello World and some of the other things between devil quotes that
we've printed out with the see out statement, those were strings, except we weren't really making
a string in a variable or anything.
You know how we've made variables with within and doubles and charges and things like that.
You can also make a string type variable, and we weren't really doing that.
We were just kind of hard coding a string by putting the double quotes in the printout statement.
So now we're going to talk about what strings really are.
And they're very closely related to erase, actually.
So that's kind of why I'm putting this lecture right after the arrays because it's kind of fresh in
your mind.
So I'm hoping that you'll be able to draw similarities between how arrays are laid out in memory and
how they work with subscript and all that and how that relates to strings.
So today I'm going to mix it up.
I'm not going to be in visual studio code.
I'm instead going to use them.
I'm actually going to do it from Bash right now.
I am in Bash.
I'm in the same directory that this just says first program, which is really not a first program.
I just keep abusing this directory and putting all these things in here.
As you can see, I already have a strings.
Does CBP file so you can just open that with them and string first CP if you don't have it, you can,
of course, just start from scratch the same way you know them and then strings that CBP will just create
a new file and then you just make sure you save it.
Or if you want to create it first without opening it, then you can do touch strings, CBP or something
like that.
That's another command touch lets you make a file so you can just be like my file here.
You know, whatever the file name is, you can just put that right there, but I already have it right
here.
So I'm going to just open that, OK?
Or so I've already just set it up with a main function like a skeleton.
And this include right here with I'll stream.
And so what we're going to do is just straight up put the data type, which is a string, and we have
access to this data type because of us having the IO stream here.
So the string data type is not really a primitive data type.
It is something that is called a class or an object.
That is something we will get into later.
But basically there's a big file or several files that have to do with the definition of this data type.
And you can actually do a lot of fancy things with strings, as we will see, but declaring it is as
simple as doing that.
So just like an entity, you can do string, you notice it doesn't highlight, and that's because it's
not primitive.
You notice I put float.
It highlights if I put double it, highlights if I put char.
It highlights greens, but string.
Since it's not a primitive type, it's something a little more complex.
It's just remains gray.
So same thing we can put a variable name just like we were before, so maybe I just call this string
s and what I can do is set it equal to something just like Hello world between the double quotes, remember?
So strings go between double quotes all the time.
Single quotes are reserved for chars.
So, you know, a child would be something like that, but a string would be something like that.
So we've used this hello world offer, of course, and our first program, but we just put it in a see
out.
You know, just we had something like, see out, hello world and then in the line.
But now we're actually saving this hello world into a variable called S.
That's the name of the variable.
And of course, here is the type that precedes it, and we use the assignment operator just like other
variables.
So what is a string, though?
So a string is actually technically an array of characters, so you can make an array of characters
right?
And you can imagine that it's just something like between here, like you had an age and an E and an
L
L, you know, I mean, I don't need to continue, but it's basically hello world kind of like that
and an array and you have these positions to like the zero one two three four.
So just like an array, you have positions in a string that are similar to an array.
You have indices and you can then use this sub scripting thing that you are using for arrays already.
And also a big, important thing to remember is that it starts at zero.
That's something we're going to be focusing on a lot.
All of these indices, when you're talking about containers, they're going to be starting at zero and
counting up from zero.
So the first element, its position is at position zero in here.
So the string is kind of laid out like this.
It's really just a bunch of characters connected, which makes sense, right?
Because H is kind of like a character, you know, E is like a character.
L is like a character.
And even the space is kind of considered like a character there.
So it's just really like a collection of a bunch of different characters.
So kind of makes sense.
And when they're put all together, you can call that a string thing is you don't have to have a collection
of a of multiple characters.
You could easily just have this be a string, just see like I was kind of showing before.
So a cool thing about strings, like I was saying is you can actually index them, and when you index
them, it will give you a character.
So I can actually make another variable and say Char C equals and I can actually say I can use s, and
I'll use this subscript operator with these brackets.
And what I'll do is, I'll say s zero and here.
So what don't you like, take a moment and think about what is going to get saved into the variable?
See if I do s- subscribe to zero.
So think about that for a second.
OK, so let's go ahead and actually print this stuff out to see what it is.
So I'm actually into this and I'll print out s, which is the string, and then I will go ahead down
here and I will print out.
See, so you can see what see is.
So I'm going to go ahead and compile this, and it kind of depends on that one interesting thing.
This may or may not work, I believe it depends on the version of C++ that you have installed, but.
You might not be able to print out.
With this output operator, a string without including another thing from Standard Temple Library besides
iOS gene.
So there's another one called string.
So you can actually go, for example, would be something like this.
It's actually string like this, just like iOS Dream.
And that gives you access to a lot of other things that have to do with strings.
You can use a lot of extra little tools, along with strings to do some kind of cool, complicated stuff.
I'm not a hundred percent sure if that is going to be necessary in the version I have running here for
my bash, but we'll have to see if it throws an error.
We will go ahead and include this.
Otherwise, I will admit it.
So I went ahead and did X there to save.
So now I'm going to do the D-plus Plus command, and I'm going to do strings that CP dash.
Oh, and I'll just call it strings.
I'll just call the strings dot out.
OK, so yeah, it has.
Oh, that is actually another problem.
That's a separate problem.
Doesn't recognize string because I wasn't using a namespace.
So if I go back in here, all I have to do really is put the study in Hong Kong before that, since
we are not using doing this using namespace instead.
We actually have to include this namespace with the scope resolution operator, just like we've talked
about before.
So I'm actually going to just use this and add this in here.
So this should fix that problem.
Let's go ahead and compile this again and look at it runs fine.
I'm going to clear and we notice that if I do an LS here or if you're not in bash, you can do there
if you're in seemed.
And we see that we have this string sort out our output file.
So I'll go ahead and run this strings down.
Read us last string start out and you might notice that instead, if you're not in bass, you might
do something like that for string start out.
But you know, I'm going to go ahead and run it standard like bash and we see this hello world string
that was s.
And then after that, we printed out an s sub zero.
So S subscript zero positions zero.
Is this h right?
So pretty interesting that we can do that, let's let's kind of see if we can print out some more.
So if I was maybe to not put this in line yet and I instead just wanted to stay on the same line, I
could do something like.
Actually, I could keep it, I'll keep it on the same line we could just to see.
And then we actually.
I'll leave this see.
So it saved Essence Subzero saving to sea, but let's just keep printing out the rest of these.
So I will go as sub one as sub to s sub three s sub four s.
And you know, I'm just going to go with was zero one two three four.
So we'll just print out hello and then I'll do Angelyne.
So this should actually match it with the characters all smashed up against each other.
It should be h e l l o, and that should print out below our hello world.
So let's go ahead and test it out.
I'm going to do I'm in escape mode now, so I'll go ahead and do a client and an X to save and quit.
Or you can do a W Q and like that.
So now it's saved on.
You go ahead and press the up key so we can re compile it.
And then for some key again, to run it again.
And now we see that we are printing all of those characters as well, so not just the age, but we're
printing out the E the L L just by using the S serve one you know, of to the best sub for there.
So you notice that position zero once again is H.
So we have h zero e as a position one.
First of all, the position to second position three and opposition four, if we kept going, it would
be five six seven eight nine 10, so that would be the total length there.
So speaking of length, what how do we get the length of this string?
So let's go back to our file, so you might be thinking about that potential way to get it with an array
using the size of function and then maybe having like the, you know, the size of the string and then
dividing by the size of like a, you know, sub zero or something like that.
It's possible to like, go about it in that way.
I'll be a little bit harder.
There's actually something that.
Is a bit easier, and it may look confusing at first because we haven't talked about much about functions
yet and gone over how to use functions, and we definitely have in our classes.
And you know, this string, like I told you, something kind of called a class.
It has a lot of data associated with it, but it gives us some nice tools to use, and that was the
point that I wanted to get across.
So instead of just concluding our stream, I'm going to uncomment this and I'm also going to include
this string library now.
So something that I can do to get these links is use the string, which is s.
So that's a variable.
And what all I have to do is use this dot and then tape lengths with some parentheses here.
So it may look kind of confusing, but this is going to give us the length of the string s, which is
and if we were counting the actual length, we're not going to start at zero.
That's just the position, right?
Because we're zero one two three four five six seven eight nine 10.
But if we start at one and we actually counting how many characters are here, it would be one two three
four five.
We're still counting this six seven eight nine 10 11.
So we're still counting the white space here and its length.
11.
So let's go ahead and test this out, I'm actually going to since it's a no, I'm going to actually
make an integer and I'm going to call this string length like this.
And I will, you know, I actually call I just say string like that, I say string l equals.
S Dodd.
So period and then the word length, and then I'll put my finishing semicolon and we have these parentheses
here, don't forget those.
This is something we will talk about later.
But essentially this is very similar to this main function you notice after the main function.
There are these parentheses.
We're going to talk about functions later, I keep kind of saying that, and you're probably like,
when are we going to talk about functions?
We will talk about functions later.
I'm just trying to see, you know, how much stuff we can really stuff into this main function before
we need to break it up and have our own functions.
But nevertheless, this is a function or something that you might often hear as a method.
You know, a method is kind of something that has the dot and then a function after it like this.
Whereas a sometimes you might hear a function referred to as something more like if you had length and
then just like the thing that you're interested in was maybe in here, like your string was inside the
parentheses.
So you may hear that terminology, but you can call it a function right now if you want.
So, you know, because that that works to you and C++, we can just think of this as a function.
And what you're doing is you're calling the function on the string just like the main gets called by
the operating system.
Remember, this is the same thing.
A function is getting called on the string.
And when I say on this because it's using this dot, we're doing X, which is the variable for the string
and then dot.
So OK, we're going to call a function on this now on the string, and we're going to call the length
function in this length function is just going to give us the length of the string, which was 11,
I believe, and it will save it.
So all this stuff that happens right here is giving us that number 11 and it's saving it here into this
integer variable.
So now we should be able to print out this integer variable and have it show us that number, right?
So let me go ahead and just do that, so I also print out this, I'll actually make a little string
here that we can print out, and it'll just be a label.
So I'm just going to say the length of the string s
and I'll put a call in here and then we will just put this string L and I will also put it in line here.
OK, so let's see what happens.
So I'm going to go ahead and compile this again.
And then I'm going to run it.
And there we see R11.
It says length of the string s and then we have a call in and then it says that it's 11.
So pretty cool, right?
And the cool thing is that there are a ton of functions that have to do with these string.
So once you include this guy right here, you get access to a ton of stuff like this where you can put
the dot and put a bunch of other functions that will do cool things for you.
And it's not always necessary to have the done you might encounter.
You might encounter some of these, too, where you'd have a function that you put the string inside
of the parentheses for as well, and it will do something for you.
So pretty cool stuff.
That is something that I am going to explain.
I think in a another lecture where I will kind of go over all the different cool functions that you
can use with strings because they're very useful and we're definitely going to want to use them to solve
some cool problems in the near future.
I don't want you to get too caught up in this funky like dot and then the function and get worried about
like, what is that?
Don't worry, we will explain that for now.
Just think of it as a nice way to get the length of a string if you can just memorize this.
And no, that OK.
Whenever I have a string variable like this is this is here, then I can just, you know, whatever
I call it, you don't have to call it as you could just call it, like, you know, my string.
And you could just do my string length.
You just put whatever the variable name is and then dot length with these empty parentheses here, and
that would give you the length.
So just remember that function for now, because it's sort of a useful function.
And I just wanted to throw one in here for this introduction to strings, but we will be going over
some more as well.
And of course, we will be explaining what these parentheses mean and what it really is to have other
functions besides this one main function that we're stuffing everything into.
OK, cool.
So now, you know, this, of course, wouldn't run, so I'd have to change this to my string.
But I think that I think that we are pretty much good with explaining the basics of the string.
So I'm not going to go change all this and I might just turn this back to this, actually.
So pretty cool.
I will have another video where I go into strings a little bit more, but I thought this was just a
cool kind of introduction.
You can practice making some variables with these, although it's fairly straightforward.
Maybe practicing the indexing would be a good idea.
A good idea as well, this is kind of just meant to be a very basic introduction to tell you what a
string really is.
Show you how it's related to an array and it really has these positions underneath it, you know, that
are like, you know, zero one two three four five six seven, eight, nine and 10 right there.
So right underneath the characters, these are the positions for the string.
And I just wanted to point that out because I feel like that's one of the most important things.
Strings go a lot deeper and we will be going deeper into it in the next few lectures.
But with that, I will let you sit on this and kind of absorb this minimal material that we've provided
so far, and I will see you in the next coming lectures.