1
00:00:00,180 --> 00:00:09,330
OK, so this is a follow up lecture about strings, that first lecture was just a really easy introduction

2
00:00:09,330 --> 00:00:11,280
into what a string is.

3
00:00:11,760 --> 00:00:19,590
And I showed you a little bit of stuff that you could do with it, like subscript dean index indexing

4
00:00:19,590 --> 00:00:25,650
to like, print out a character in a certain position like the H.

5
00:00:25,800 --> 00:00:30,860
You know what position zero or the position one or something like that.

6
00:00:30,870 --> 00:00:36,450
We did the whole hello, I remember, but then I showed you the length as well, right?

7
00:00:36,480 --> 00:00:37,920
Escort length.

8
00:00:38,250 --> 00:00:43,440
We use that length function, and I said I would mention a little bit more about other functions and

9
00:00:43,440 --> 00:00:44,370
stuff like that.

10
00:00:44,940 --> 00:00:49,740
I have decided that I'm actually not going to drop a bunch of functions on you.

11
00:00:50,790 --> 00:00:59,220
I think that it would just kind of confuse you and wouldn't really help as much at this point, since

12
00:00:59,220 --> 00:01:03,930
we're trying to just learn how to program and be better programmers now.

13
00:01:03,990 --> 00:01:09,930
Think later on you could go look at the extensive documentation of these strings, and I will point

14
00:01:09,930 --> 00:01:13,890
you towards that later on so you can use some more advanced string functions.

15
00:01:13,890 --> 00:01:18,180
But the thing is is we're trying to just learn how to mess with strings.

16
00:01:18,180 --> 00:01:22,620
We will encounter them in some of the problems that we will be solving very soon.

17
00:01:23,040 --> 00:01:30,180
And so I think what's better is to just introduce only two more things at this point in time about strings,

18
00:01:30,450 --> 00:01:37,050
which are actually instead of functions going to be more associated with the operators.

19
00:01:38,370 --> 00:01:41,040
So that is what we are going to be doing.

20
00:01:41,670 --> 00:01:49,680
So the first thing that I want to cover is about digging a little bit more into the subscript operator.

21
00:01:49,690 --> 00:01:56,280
So remember that we did something like s sub zero just indexing the position zero.

22
00:01:56,520 --> 00:02:02,970
And we actually, you know, did a C out with that and we were able to print this out and it was an

23
00:02:03,000 --> 00:02:09,030
h right because position zero was h and then one, two, three, four or five and so on, right?

24
00:02:10,080 --> 00:02:11,610
So that was cool and all.

25
00:02:11,700 --> 00:02:16,110
But that's not the only thing you can do with this.

26
00:02:16,650 --> 00:02:21,210
Something pretty cool is that if you want, you can actually change.

27
00:02:22,500 --> 00:02:30,990
Some of the individual characters in here, so let's say I wanted to change the E and Hello world to

28
00:02:30,990 --> 00:02:31,860
an A..

29
00:02:32,490 --> 00:02:44,610
I can actually do that by using a combination of this subscript operator and using the assignment operator

30
00:02:45,990 --> 00:02:46,860
like this.

31
00:02:47,490 --> 00:02:54,360
And on the right side of the assignment operator, I can actually put whatever character I want to be

32
00:02:55,320 --> 00:02:57,030
written to that position.

33
00:02:57,210 --> 00:03:03,390
So if I want to write an A., This would overwrite the H in position zero, but I can change this to

34
00:03:03,390 --> 00:03:03,840
one.

35
00:03:04,200 --> 00:03:06,050
And this would overwrite the E.

36
00:03:06,060 --> 00:03:12,900
So what this statement is saying here is, OK, we're looking at the string s, which is a variable

37
00:03:12,900 --> 00:03:13,200
rate.

38
00:03:13,200 --> 00:03:14,550
It's a variable type string.

39
00:03:14,550 --> 00:03:15,720
It contains hello world.

40
00:03:16,630 --> 00:03:18,190
What does it position won in that?

41
00:03:18,220 --> 00:03:18,670
OK.

42
00:03:19,090 --> 00:03:23,290
We're using these brackets, so that means position one, let's go there and check it out.

43
00:03:23,560 --> 00:03:24,550
This is zero.

44
00:03:24,820 --> 00:03:25,640
This is one.

45
00:03:25,660 --> 00:03:26,800
OK, position one.

46
00:03:26,800 --> 00:03:27,520
There's an E.

47
00:03:27,790 --> 00:03:30,380
What are we doing with this position?

48
00:03:30,400 --> 00:03:32,590
Well, we're using the assignment operator.

49
00:03:32,620 --> 00:03:37,420
So what we're going to do is say, take this character note the single quotes around it.

50
00:03:37,430 --> 00:03:38,620
This is a character.

51
00:03:38,890 --> 00:03:40,150
That's why they're single quotes.

52
00:03:40,750 --> 00:03:46,330
We're going to take this character a we're going to assign it to this position.

53
00:03:46,330 --> 00:03:48,760
One in the string hello world.

54
00:03:49,060 --> 00:03:51,320
So overwrite the E with an A..

55
00:03:52,990 --> 00:03:56,150
So let's actually confirmed that's what happens.

56
00:03:56,170 --> 00:04:03,490
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

57
00:04:03,490 --> 00:04:06,850
the before and seven is that after we change to a.

58
00:04:07,240 --> 00:04:09,430
So let's go ahead and let's

59
00:04:12,580 --> 00:04:16,390
clear the screen and I'm going to compile this.

60
00:04:17,380 --> 00:04:21,700
And so you notice that I'm doing the G Plus Plus doing so secret Peter Erskine stuff out.

61
00:04:22,270 --> 00:04:23,390
You can call it whatever you want.

62
00:04:23,410 --> 00:04:24,310
And now I'm running it.

63
00:04:24,850 --> 00:04:27,460
And we noticed that it actually gets changed.

64
00:04:27,670 --> 00:04:31,540
So it said Hello world, and the first one and the second one says, Hello world.

65
00:04:31,540 --> 00:04:34,870
With that, a so pretty cool, right?

66
00:04:36,400 --> 00:04:38,080
But there is more.

67
00:04:38,830 --> 00:04:47,350
So before I go onto this next thing, I want to go over, though I want to kind of let us have a chance

68
00:04:47,350 --> 00:04:52,900
to use some of the things we have learned in conjunction with these new things.

69
00:04:53,710 --> 00:05:04,210
So what I want you to do is pretend that you don't know how long this string is and you might be saying,

70
00:05:04,210 --> 00:05:07,030
Well, why would I not know how long it is?

71
00:05:07,420 --> 00:05:09,310
It's right here in front of me.

72
00:05:09,310 --> 00:05:10,420
It's hello world.

73
00:05:10,420 --> 00:05:11,820
It's blatantly length.

74
00:05:11,830 --> 00:05:16,810
11, you know, one two three four five six seven eight nine, 10 11.

75
00:05:17,650 --> 00:05:18,700
I know how long it is.

76
00:05:19,060 --> 00:05:24,340
Well, what's going to happen in the near future in this course is that we are going to stop relying

77
00:05:24,340 --> 00:05:32,830
on writing our own data in the files, and we are actually going to be taking in data from the user

78
00:05:33,190 --> 00:05:36,660
where the user enters the input on the keyboard.

79
00:05:36,670 --> 00:05:43,450
So what will happen is the we it it doesn't necessarily have to be another user, it'll be us testing

80
00:05:43,450 --> 00:05:43,920
it out.

81
00:05:43,930 --> 00:05:50,260
But we will run the program and the program will wait for us to type stuff on the keyboard.

82
00:05:50,710 --> 00:05:55,320
And that stuff we type will be the data that goes into our program.

83
00:05:55,330 --> 00:06:03,520
So rather than this hello world right here, it might be us typing it on the keyboard when the program

84
00:06:03,520 --> 00:06:09,100
runs instead of typing it straight into the file right now while we're making the program.

85
00:06:10,270 --> 00:06:17,320
So if you do that, you won't really know how long the input is, I can type like a super long sentence

86
00:06:17,320 --> 00:06:23,980
or something, and if I want to create a string in the program that has that thing that was typed,

87
00:06:25,090 --> 00:06:33,780
then I will be not knowing that its length 11, you know, it might be length like 20 or length 100

88
00:06:34,300 --> 00:06:35,220
length a thousand.

89
00:06:36,700 --> 00:06:42,970
So with that in mind, let's just pretend that that is what's going on.

90
00:06:43,180 --> 00:06:48,220
We haven't talked about how to get input from the user yet, but that's coming up really soon.

91
00:06:49,120 --> 00:06:54,580
So let's just pretend right now, though, that we don't know how long this string, as is.

92
00:06:54,580 --> 00:06:56,440
We don't know what it contains.

93
00:06:57,410 --> 00:06:58,800
We don't know how long it is.

94
00:06:58,820 --> 00:07:01,970
We just know that the string is s and it has a string in there.

95
00:07:03,320 --> 00:07:12,620
How would you change the last character of the string to the letter A.?

96
00:07:12,800 --> 00:07:15,790
So in this case, it will be D..

97
00:07:16,430 --> 00:07:20,390
But what I'm saying is that you're not allowed to do something like this.

98
00:07:22,110 --> 00:07:28,800
You can't be like, you know, as of 10, because that would be a position 10, right, you cannot do

99
00:07:29,010 --> 00:07:30,510
as of 10 equals a.

100
00:07:31,290 --> 00:07:36,810
So not allowed to do that because you have to pretend like you don't know how long it is.

101
00:07:36,840 --> 00:07:38,040
How are you going to figure that out?

102
00:07:38,190 --> 00:07:39,100
What are you going to use?

103
00:07:39,120 --> 00:07:45,930
What did we just learn and think about what we just learned in the last lecture and how you could do

104
00:07:45,930 --> 00:07:46,380
this?

105
00:07:47,610 --> 00:07:53,070
I think about things that we learned to lectures and got like the lecture before last lecture, so I'm

106
00:07:53,460 --> 00:07:59,130
I'm going to kind of implore you to pause the video and see if you can figure this out on your own.

107
00:07:59,850 --> 00:08:03,180
Otherwise, I'm going to solve it for you right now.

108
00:08:03,180 --> 00:08:06,000
So if you can pause the video and see if you can do this.

109
00:08:08,680 --> 00:08:14,770
OK, so I am actually going to go over the solution now and show you a couple of different versions

110
00:08:14,770 --> 00:08:15,070
of it.

111
00:08:15,850 --> 00:08:22,150
So let's think about what the statement of this problem was a very simple problem.

112
00:08:22,510 --> 00:08:24,850
We'll be getting into harder ones later, of course.

113
00:08:25,510 --> 00:08:32,530
So what I said was, I want you to change the last character of the string as pretending it could be

114
00:08:32,530 --> 00:08:34,240
any string, any length.

115
00:08:35,170 --> 00:08:38,200
In this case, it's hello world, but we're trying to pretend that we don't know it's hello.

116
00:08:39,790 --> 00:08:42,880
I want you to change the last character to the letter A..

117
00:08:44,180 --> 00:08:49,090
So one thing that we can use, and I'm going to comment this out right now.

118
00:08:50,800 --> 00:08:54,220
One thing that we learned was.

119
00:08:55,180 --> 00:09:02,290
How to get the length right so we can get the length with escort length.

120
00:09:04,850 --> 00:09:08,240
So start length, what would that give us?

121
00:09:08,860 --> 00:09:15,410
We think about it, it gives us 11, right, because this is 11 one two three four five six seven eight

122
00:09:15,410 --> 00:09:16,550
nine 10 11.

123
00:09:16,760 --> 00:09:19,190
Notice that I started counting at one, though.

124
00:09:20,930 --> 00:09:26,540
When we look at positions in the string, we don't start counting at one, do we start counting at zero,

125
00:09:26,540 --> 00:09:32,890
so zero one two three four five six seven eight nine 10 and that's kind of a little heartless.

126
00:09:32,900 --> 00:09:38,210
I said, you know, you cannot go like s of 10 equals a.

127
00:09:39,930 --> 00:09:48,080
So we know that the last character, D, is actually at 10, but we can't hard code that we know the

128
00:09:48,080 --> 00:09:52,550
length is 11 and it's 11 because we start counting at one.

129
00:09:52,550 --> 00:09:55,970
When we're doing the length rather than counting, it is zero for the position.

130
00:09:56,570 --> 00:10:06,950
What this means is that the last character in a string is always going to be one less than the length

131
00:10:07,520 --> 00:10:13,280
because if you start counting at one instead of zero, that is just saying the same thing as you're

132
00:10:13,280 --> 00:10:15,880
basically adding one to each position.

133
00:10:15,890 --> 00:10:21,300
So if this is a position 10, you add one to it.

134
00:10:21,440 --> 00:10:27,440
Basically, you know, for it to be the length you go backwards for the other direction.

135
00:10:27,440 --> 00:10:31,580
So the last position is at length minus one.

136
00:10:31,580 --> 00:10:33,530
So that length minus one.

137
00:10:35,420 --> 00:10:42,710
So something like this, what I want to do is I want to save this to a variable, though, because I

138
00:10:42,710 --> 00:10:43,850
want to be able to use it.

139
00:10:43,850 --> 00:10:48,080
So I'm going to make an integer because start length is a number, right?

140
00:10:48,470 --> 00:10:50,590
We saw before that, it gave us 11.

141
00:10:51,470 --> 00:11:00,380
So I'm going to say string length and string length equals escalates.

142
00:11:01,730 --> 00:11:03,680
That gives me the length.

143
00:11:03,710 --> 00:11:05,620
But I wanted minus one, right?

144
00:11:05,630 --> 00:11:11,940
So what I could do is say, and string length equals escort length minus one.

145
00:11:12,170 --> 00:11:13,490
Yeah, I can do that.

146
00:11:13,490 --> 00:11:14,960
Pretty cool, right?

147
00:11:15,260 --> 00:11:22,340
I can actually use more than just start length and on the same line, I can use some math here if this

148
00:11:22,340 --> 00:11:24,860
is kind of seeming like a little too much for you.

149
00:11:24,890 --> 00:11:27,350
You don't have to put this on the same line if you like.

150
00:11:27,680 --> 00:11:31,070
You can just say string length equals escort length.

151
00:11:32,360 --> 00:11:33,680
And that kind of makes sense, right?

152
00:11:33,680 --> 00:11:37,520
Because we call it string length, we didn't call it last position or something.

153
00:11:37,550 --> 00:11:39,080
So this is the length of the string.

154
00:11:39,080 --> 00:11:47,210
If we wanted to do that length minus one, it might be more appropriate to name this variable like last

155
00:11:47,360 --> 00:11:48,980
position or something like that.

156
00:11:49,910 --> 00:11:55,460
I'm going to leave it as it's I'm going to leave it as just a start length.

157
00:11:56,750 --> 00:11:58,940
So what do we need to do now, though?

158
00:11:58,940 --> 00:12:03,820
We need to subtract from it so we can do this a few ways and you should know how to do this.

159
00:12:03,830 --> 00:12:07,760
How do we subtract one from a variable?

160
00:12:08,810 --> 00:12:14,660
So you notice that we did something like i1 equals i1 minus one?

161
00:12:16,300 --> 00:12:20,860
That's something we did, but you notice that you're actually able to replace that with the minus equals

162
00:12:20,860 --> 00:12:27,460
so we can actually do string length minus equals one.

163
00:12:28,960 --> 00:12:32,170
You could also, if you want, you could do it the long way.

164
00:12:32,680 --> 00:12:34,930
Right, we've talked about string length on this one.

165
00:12:35,740 --> 00:12:40,570
But what's something else we can do notice we're only doing minus one right here, so we can even make

166
00:12:40,570 --> 00:12:43,530
a shorter version of this, that shorter than both of those?

167
00:12:43,540 --> 00:12:47,370
And that is just minus minus string length.

168
00:12:47,380 --> 00:12:53,200
I remember that that is just the prefix decrement operator.

169
00:12:54,040 --> 00:12:55,880
So we don't want to do.

170
00:12:55,900 --> 00:12:58,180
We're not going to do a string length minus minus.

171
00:12:58,430 --> 00:13:03,640
We're just going to do minus minus string like to be sure that it's happening right now as we should

172
00:13:03,640 --> 00:13:09,280
do it with the prefix rather than the postfix, so that will subtract one from it.

173
00:13:10,060 --> 00:13:10,930
And now what?

174
00:13:11,590 --> 00:13:18,850
So now we have string length actually equaling something that is at the right position.

175
00:13:18,850 --> 00:13:20,750
It's at position 10, you know?

176
00:13:20,770 --> 00:13:27,880
But more importantly, it's at the last position of whatever as is because we know that if the length

177
00:13:27,880 --> 00:13:31,570
was greater than it would still work and I will show you that as well.

178
00:13:32,440 --> 00:13:37,660
So now that we have the position, all we have to do is use this right here.

179
00:13:37,670 --> 00:13:39,150
We just need to do this line.

180
00:13:39,160 --> 00:13:42,220
We need to like index it, but we're not going to index it at one, right?

181
00:13:42,910 --> 00:13:50,050
We're going to index it at the string length because string length is a variable that holds the correct

182
00:13:50,140 --> 00:13:51,010
number.

183
00:13:52,300 --> 00:13:58,920
So might seem a little confusing, but you don't have to put a number in here like a hard coded number.

184
00:13:59,380 --> 00:14:06,520
This is basically a number now, right, because it was 11 right here after we did this line.

185
00:14:07,700 --> 00:14:12,320
It was 11, for this example, and then we did a minus minus, and now it holds 10.

186
00:14:12,920 --> 00:14:24,920
So essentially something like this as 10 equals a which I told you not to do that is the same thing

187
00:14:25,580 --> 00:14:27,410
as doing this.

188
00:14:31,790 --> 00:14:32,840
So pretty cool, right?

189
00:14:32,870 --> 00:14:34,490
This is basically this string length.

190
00:14:34,490 --> 00:14:38,600
You can imagine it being a number 10 because that's what it stores inside of it.

191
00:14:38,600 --> 00:14:42,470
From this line right here, that was 11, it turns to 10.

192
00:14:42,710 --> 00:14:48,080
Now we use the number 10, but trapped in this variable here, which could be anything, right?

193
00:14:48,290 --> 00:14:51,650
If this was a bigger string, this might not be 10.

194
00:14:51,680 --> 00:14:54,260
It could be any number, but we know that it's the right position.

195
00:14:55,940 --> 00:14:58,280
So pretty cool that we can do that.

196
00:14:58,290 --> 00:15:03,850
Let's go ahead and put some see out statements so we can actually confirm what's going on here.

197
00:15:03,860 --> 00:15:10,820
So what I'm going to do is I'm going to print out the string length.

198
00:15:14,470 --> 00:15:20,980
And in the line, and you know what I might do, this is a minus minus string length, but

199
00:15:23,770 --> 00:15:26,140
I'm just maybe going to call this.

200
00:15:28,910 --> 00:15:37,370
Last position just because here when I minus minus, it really becomes the last position, right?

201
00:15:37,670 --> 00:15:42,250
I think that's a better name for it since we're doing a minus minus here, right here.

202
00:15:42,260 --> 00:15:48,800
It could be the string length in what might be better to do last position minus one, but I want you

203
00:15:48,800 --> 00:15:52,230
to be able to use your minus minus separator and stuff like that.

204
00:15:52,250 --> 00:15:57,760
The decrement operator and and separately use the length and kind of make it nice for you.

205
00:15:57,830 --> 00:16:00,530
You can, of course, do it all on one line and you can just do that.

206
00:16:03,230 --> 00:16:07,610
So I'm going to call in that last position, so I'm going to change this as well.

207
00:16:10,070 --> 00:16:11,270
So it makes more sense.

208
00:16:11,660 --> 00:16:15,680
We print out now this is the last position.

209
00:16:17,930 --> 00:16:20,780
And then what we're going to do is also here.

210
00:16:20,780 --> 00:16:21,650
Print this out.

211
00:16:21,650 --> 00:16:27,050
So I'm actually going to copy this and paste it here.

212
00:16:29,900 --> 00:16:34,910
And I just did that, you know, not really going to a huge room tutorial, but what you can do is go

213
00:16:34,910 --> 00:16:38,030
to the front of this if you want to copy paste big chunks of code.

214
00:16:38,510 --> 00:16:42,170
I pressed V, you notice that it says visual down here.

215
00:16:42,620 --> 00:16:44,240
So this is visual block mode.

216
00:16:44,840 --> 00:16:50,660
And then when you press W., it goes to the end of a word, so you can just press W a few times and

217
00:16:50,660 --> 00:16:53,270
you notice it's like jumping to the end of each one of these words.

218
00:16:54,440 --> 00:16:59,600
The cursor, although this is not highlighted here in the semicolon, since the cursor is under it,

219
00:16:59,600 --> 00:17:01,580
it's going to capture this as well.

220
00:17:01,580 --> 00:17:06,710
When we copy it, to copy it, you press y, which stands for a yank.

221
00:17:07,220 --> 00:17:10,190
So I press that and it yanks it.

222
00:17:10,940 --> 00:17:14,000
Then I'm I'm like, you know, making a new line.

223
00:17:14,000 --> 00:17:15,470
But now I'm in escape mode.

224
00:17:15,680 --> 00:17:16,790
You know, it's command mode.

225
00:17:16,790 --> 00:17:23,270
I can move around and I press p in that piece that I'm going to undo that, though, because I don't

226
00:17:23,270 --> 00:17:23,780
want to do that.

227
00:17:25,040 --> 00:17:25,670
I already did it.

228
00:17:26,840 --> 00:17:32,840
So we're going to print out the last position, which you know right here should be 11.

229
00:17:32,840 --> 00:17:37,690
So we'll print out 11 and then we do a minus minus, so it becomes 10 on this line.

230
00:17:37,710 --> 00:17:39,770
Then we print it out, so it should say 10.

231
00:17:40,370 --> 00:17:46,580
Then we change whatever position 10, which should be the D and then we print that out.

232
00:17:48,530 --> 00:17:54,380
So let's go ahead and test this out and we go ahead and compile it and run it.

233
00:17:55,990 --> 00:17:58,660
OK, so pretty cool, we had Hello, World.

234
00:17:59,410 --> 00:18:06,340
This is the last position when it was 11 and last position when it was 10, I'm actually going to put

235
00:18:06,340 --> 00:18:09,100
something here just so we know that that's what it is.

236
00:18:09,110 --> 00:18:25,360
So last position equals and I'll put that here to settle last position equals and then now we can compile

237
00:18:25,360 --> 00:18:26,410
it and run again.

238
00:18:26,410 --> 00:18:28,920
And now you notice his last position equals 11.

239
00:18:28,930 --> 00:18:34,630
Then we did a minus minus, the last position equals 10, and then we change whatever in position 10

240
00:18:34,630 --> 00:18:35,500
or change.

241
00:18:35,500 --> 00:18:41,170
When I was at the last position, which was the D two and A and we see our result here.

242
00:18:42,580 --> 00:18:43,580
So pretty cool, right?

243
00:18:43,600 --> 00:18:47,980
We're learning how to do a lot more stuff, which, you know, this is really interesting.

244
00:18:50,500 --> 00:18:56,230
So you know, this is a pretty powerful thing that you can change this.

245
00:18:56,530 --> 00:19:07,060
Another thing that you might want to do with the length is be able to kind of check things like you

246
00:19:07,060 --> 00:19:09,850
can use the, you know, an if statement.

247
00:19:09,850 --> 00:19:12,340
So we could combine some more stuff with this.

248
00:19:12,730 --> 00:19:19,090
And let's just kind of add another little condition to our program.

249
00:19:19,390 --> 00:19:27,400
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

250
00:19:27,400 --> 00:19:29,830
D, then we will change it to an A..

251
00:19:30,160 --> 00:19:32,950
But if it is a D, we will change it to an X.

252
00:19:33,310 --> 00:19:35,160
So let's go ahead and see if we can do that.

253
00:19:35,170 --> 00:19:41,680
That way, we can combine our knowledge of conditional statements to to handle this.

254
00:19:41,800 --> 00:19:47,290
So I'm going to put an f here, but let's see if you can do this on your own.

255
00:19:47,290 --> 00:19:50,500
So what I am giving you is another really small challenge.

256
00:19:51,850 --> 00:19:54,010
Now you kind of know how to change the character.

257
00:19:54,580 --> 00:20:03,610
What I want you to do is use f and else, and I want you to change the last character of the string

258
00:20:03,610 --> 00:20:06,010
s to an A..

259
00:20:06,340 --> 00:20:13,060
If the last character is currently a D, otherwise or else, I want you to change the last character

260
00:20:13,060 --> 00:20:16,540
to an X. So go ahead and pause the video and see if you can do that.

261
00:20:19,120 --> 00:20:23,470
OK, so I'm going to go ahead and tell you how to do this if you haven't gotten it already.

262
00:20:24,880 --> 00:20:34,180
So what we're going to do is say, all right, so if the last position of the string so s index last

263
00:20:34,180 --> 00:20:46,270
position, if that is equal, equal to right, that's how we check a quality d, then.

264
00:20:48,290 --> 00:20:52,760
What we're going to do is put this last position equals a.

265
00:20:53,450 --> 00:20:58,760
So I'm actually going to paste that here, and that didn't work out well with my paste,

266
00:21:01,670 --> 00:21:03,050
and we'll

267
00:21:06,140 --> 00:21:07,040
clean that up a bit.

268
00:21:07,520 --> 00:21:14,510
So if that's the case, if it is a D, so I use this equal equal.

269
00:21:14,510 --> 00:21:20,330
Remember, this is the equality operator, then I want to change it to an a.

270
00:21:21,140 --> 00:21:32,480
Otherwise, I'm going to say else and then I'm going to do as a last position is equal to X kind of

271
00:21:32,480 --> 00:21:32,900
pointless.

272
00:21:32,900 --> 00:21:35,840
I mean, this isn't really doing it doesn't really make sense, of course.

273
00:21:36,110 --> 00:21:41,420
Why would we change it to hello world or hello world or something like that?

274
00:21:41,750 --> 00:21:45,400
But nevertheless, it kind of exercises the things that we've learned, right?

275
00:21:46,190 --> 00:21:48,080
So congratulations if you're going to get this.

276
00:21:48,260 --> 00:21:49,310
If you weren't able to.

277
00:21:49,340 --> 00:21:52,820
Don't worry, it will come soon enough and just need some more practice.

278
00:21:53,180 --> 00:21:57,740
So the more you drill this, the better you'll get, the more you can combine.

279
00:21:57,740 --> 00:22:01,610
The things that you've learned so far, the better you will be.

280
00:22:01,610 --> 00:22:06,340
So practice, practice, practice and I will come up with more problems in the very near future.

281
00:22:06,350 --> 00:22:08,300
We're going to go over a lot of different problems.

282
00:22:08,420 --> 00:22:13,340
I'll kind of set out for you to learn this stuff better, but we just need to add a few more things

283
00:22:13,340 --> 00:22:14,960
to our toolbox, if you know what I mean.

284
00:22:16,580 --> 00:22:18,140
So pretty cool.

285
00:22:18,170 --> 00:22:23,300
Now we figured out how to use conditionals and change these things based on the conditions.

286
00:22:25,040 --> 00:22:30,710
So that's kind of all I have to say about the subscript operator being used with strings.

287
00:22:30,980 --> 00:22:39,130
Also, hopefully you understand the fact that this is very similar to an array of characters.

288
00:22:39,140 --> 00:22:41,750
It's just like a character here, character here.

289
00:22:42,500 --> 00:22:52,490
Like it has all these positions, just like an array technically in the C language you don't have access

290
00:22:52,490 --> 00:22:54,170
to and data type called a string.

291
00:22:54,560 --> 00:22:58,880
So you have to make your own strings, which are called C style strings.

292
00:22:59,510 --> 00:23:05,030
You can actually use those C strings in C++ as well.

293
00:23:05,780 --> 00:23:16,730
And those are like literal arrays of characters the way that the string data type does.

294
00:23:17,360 --> 00:23:21,020
This array of characters is kind of like that.

295
00:23:21,020 --> 00:23:22,640
It is an array of characters.

296
00:23:24,230 --> 00:23:31,550
I think it does like a constant array of characters, but we're kind of not focusing on that right now.

297
00:23:31,580 --> 00:23:38,180
Later on, I think we will get more into the internal representation in how to use C strings.

298
00:23:39,320 --> 00:23:41,080
It is more difficult.

299
00:23:41,090 --> 00:23:47,540
You have to do more work on your own and that has to do a little bit with like pointers in memory management,

300
00:23:47,540 --> 00:23:50,570
which are other topics that we're not yet getting into.

301
00:23:50,580 --> 00:23:55,100
So that's why I don't want to mention it right now, because I feel like it might be a little overkill.

302
00:23:55,940 --> 00:23:58,940
What I want to do is just introduce you to this string.

303
00:24:01,170 --> 00:24:05,580
Data type and show you some cool things that you can use.

304
00:24:06,710 --> 00:24:16,230
Like I said, there's a lot more of this dart stuff, you know, that length is just one of many functions,

305
00:24:16,240 --> 00:24:20,290
many member functions you can use with the strings.

306
00:24:21,530 --> 00:24:28,070
So later on, we will take a deeper look at those, but right now, I don't want to overload you with

307
00:24:28,070 --> 00:24:34,310
like 20 different member functions that you won't even necessarily need to use in the upcoming session.

308
00:24:34,640 --> 00:24:43,340
We want to focus on using these more primitive tools that we have, like the subscript operator and

309
00:24:43,340 --> 00:24:45,730
things like that to manipulate strings.

310
00:24:45,770 --> 00:24:50,030
So that's why we're focusing on it in this way.

311
00:24:50,240 --> 00:24:58,790
That's why I am kind of not showing you how deep the string rabbit hole goes, but you are welcome to

312
00:24:58,790 --> 00:24:59,900
explore it on your own.

313
00:25:00,140 --> 00:25:04,700
And later on, I will be going deeper into all the stuff you can use with strings.

314
00:25:04,700 --> 00:25:11,840
All of the methods, the functions and how to use the rest of the operators, as well as how to use

315
00:25:11,870 --> 00:25:14,900
C style strings from the C programming language.

316
00:25:16,040 --> 00:25:19,070
OK, so I felt like it was important that I at least mention that.

317
00:25:19,070 --> 00:25:25,190
But let's bring our focus back to just what we've learned here with this subscript operator.

318
00:25:25,760 --> 00:25:28,420
We now know that we can print stuff out with this.

319
00:25:28,430 --> 00:25:32,780
We can also use it to change things inside of our string.

320
00:25:33,560 --> 00:25:41,450
We know that we can use the length and subtract one to be able to get the last position as well.

321
00:25:42,600 --> 00:25:45,450
So pretty cool, we also used some conditionals here.

322
00:25:45,930 --> 00:25:51,210
So now I'm going to go ahead and move on to the other thing that I want to show you about strings,

323
00:25:51,210 --> 00:25:53,040
and it also uses an operator.

324
00:25:54,200 --> 00:25:55,770
And what it does.

325
00:25:55,790 --> 00:26:04,310
I'm going to go ahead and actually remove all of this stuff right here because you don't need that anymore.

326
00:26:06,530 --> 00:26:17,510
So this next thing is what we can use to add two strings together, when I say add, I really mean squish

327
00:26:17,510 --> 00:26:18,200
them together.

328
00:26:18,200 --> 00:26:22,240
So let's say that you didn't just want hello world.

329
00:26:22,580 --> 00:26:28,820
Let's say you suddenly are like, Well, I want it to not just say hello world, but I want to change

330
00:26:28,820 --> 00:26:33,830
it to say hello world again or something like that.

331
00:26:34,670 --> 00:26:42,920
So this is something you can do using an operator that you've already learned, and that is going to

332
00:26:42,920 --> 00:26:45,920
be the plus equal operator.

333
00:26:46,280 --> 00:26:53,420
So you might be thinking, that's weird, because when I used plus equal Alice using it to add stuff

334
00:26:53,420 --> 00:26:59,240
to an already existing thing kind of like that I one plus equals five.

335
00:27:00,170 --> 00:27:03,430
You know, that added five to I one.

336
00:27:04,220 --> 00:27:11,840
So the thing is, is that when you're using it with strings, it's more like tacking on things to the

337
00:27:11,840 --> 00:27:12,250
end.

338
00:27:12,260 --> 00:27:20,000
So if I had like, you know, some string that was like, you know?

339
00:27:21,080 --> 00:27:30,800
T h and I wanted to tack on E to it like another string to make the word the.

340
00:27:31,370 --> 00:27:38,200
Then I could do that by using the plus equal operator similar to how we did with the numbers.

341
00:27:38,210 --> 00:27:41,090
Yet it's going to just take this E..

342
00:27:41,390 --> 00:27:48,350
And when you're thinking about the plus as addition, the adding really means add it onto the end of

343
00:27:48,350 --> 00:27:51,380
whatever exists already.

344
00:27:52,760 --> 00:28:03,380
So you're going to do it something like this, so you would be like X Plus equals, and I could just

345
00:28:03,380 --> 00:28:05,570
say like this like s.

346
00:28:06,830 --> 00:28:08,990
So now I changed it to Hello world.

347
00:28:09,890 --> 00:28:12,710
So let's go ahead and see what this looks like.

348
00:28:16,320 --> 00:28:19,140
I'm actually going to clear before I run this.

349
00:28:20,560 --> 00:28:26,440
Of course, so you notice that what it did is it took that s and it just tacked it right on to the end.

350
00:28:26,710 --> 00:28:30,690
This is something called concatenation or a pindi.

351
00:28:31,480 --> 00:28:34,370
So you can think of it as both of those things.

352
00:28:34,390 --> 00:28:42,910
Interestingly enough, the string, a data type, has one of those dot functions, just like dot length,

353
00:28:43,210 --> 00:28:46,390
which appends to the end of it as well.

354
00:28:46,400 --> 00:28:52,870
But I feel like right now I just I'm going to introduce the operator and focus on us using that because

355
00:28:52,870 --> 00:28:56,860
we've already discussed the +Y cooperator and it's good that we can use it.

356
00:28:59,290 --> 00:29:04,390
So if I wanted to say hello world again, I could do the same thing and just put a space.

357
00:29:04,390 --> 00:29:06,940
And then then again, why the space?

358
00:29:07,330 --> 00:29:12,220
Because if I don't put this space, it just smashes it right up against the end of it, just like the

359
00:29:12,220 --> 00:29:12,580
SW.

360
00:29:12,580 --> 00:29:15,760
So you notice, if I do that, I compile it.

361
00:29:15,970 --> 00:29:16,450
I run.

362
00:29:16,450 --> 00:29:18,370
It is like, Hello world again.

363
00:29:18,400 --> 00:29:23,230
You know, I want that space in there and the space is recognized.

364
00:29:23,230 --> 00:29:24,670
It's not like it's not recognized.

365
00:29:24,670 --> 00:29:27,790
So if I do this, I compile it.

366
00:29:27,790 --> 00:29:28,850
I run it again.

367
00:29:28,870 --> 00:29:33,010
Now we have a nice space between the weird world and the word again.

368
00:29:36,170 --> 00:29:44,920
So also a really cool thing, so this is basically all I'm going to go over at this point with strings,

369
00:29:44,930 --> 00:29:49,520
I kind of mentioned a few things that we can get into later about strings.

370
00:29:49,880 --> 00:29:53,540
I like to mention these things that we haven't done quite yet.

371
00:29:54,020 --> 00:29:58,050
Just in case some of you are like, OK, you know, let's get a move on.

372
00:29:58,070 --> 00:30:04,130
I actually know about strings a little bit, but I want to, like, go deep with them and no more.

373
00:30:04,730 --> 00:30:07,910
I'm just kind of like pointing that out if you really want to jump ahead.

374
00:30:08,240 --> 00:30:15,260
The thing is, I don't recommend it if you're starting out beginning programming from scratch with this

375
00:30:15,260 --> 00:30:23,990
course, because the main important focus right now is us learning how to become good programmers and

376
00:30:23,990 --> 00:30:27,830
utilizing the C++ language to do that.

377
00:30:28,190 --> 00:30:31,760
We don't need to be power users of C++ yet.

378
00:30:31,770 --> 00:30:37,130
You don't need to be someone who's like answering Stack Overflow questions for C++.

379
00:30:38,000 --> 00:30:43,790
We're just using it as a tool to help us become competent programmers.

380
00:30:44,690 --> 00:30:52,010
You don't need to know the entire string library, all the member functions about constructors and C

381
00:30:52,010 --> 00:30:56,510
strings and the array of characters and how those are organized quite yet.

382
00:30:57,230 --> 00:31:04,190
We want to know about strings because knowing this basic amount that we've just talked about gives us

383
00:31:04,190 --> 00:31:09,650
the ability to create interesting problems to solve and the more problems that we solve.

384
00:31:09,860 --> 00:31:11,750
The better programmers we become.

385
00:31:12,140 --> 00:31:20,090
These string problems are great for us figuring out how to manipulate, modify, take in and do things

386
00:31:20,090 --> 00:31:27,170
with data and the fact that they behave kind of like arrays with these positions like zero one, two

387
00:31:27,170 --> 00:31:31,990
three and so on makes us able to do some interesting things with them.

388
00:31:32,000 --> 00:31:39,740
And since we're working with a medium that is an English spoken language or whatever language, it doesn't

389
00:31:39,740 --> 00:31:45,770
have a character set of other languages that, you know, like Russian or Chinese or anything in this

390
00:31:45,770 --> 00:31:50,780
course, of course, but it's nice for us to look at something familiar.

391
00:31:51,200 --> 00:31:55,310
If you are familiar with the English language, hopefully the way this course would be hard for you,

392
00:31:56,600 --> 00:32:01,550
then we can use this thing that we're familiar with to come up with interesting problems, and it will

393
00:32:01,550 --> 00:32:04,680
help us wrap our minds around how to go about solving these problems.

394
00:32:04,700 --> 00:32:09,800
These problems will relate to a lot of stuff and pure science, so that is why I introduce just a little

395
00:32:09,800 --> 00:32:10,700
bit on strings.

396
00:32:11,210 --> 00:32:17,390
So we can just take this a little bit and we can use this a little bit to create our own solutions,

397
00:32:17,390 --> 00:32:21,950
rather than just using some type of function that does everything for us right now.

398
00:32:23,360 --> 00:32:29,390
Kind of a long winded explanation for why I'm doing this, but I wanted to say that to put that out

399
00:32:29,390 --> 00:32:35,600
there, in case you're wondering why we're not going off the deep end down the rabbit hole into the

400
00:32:35,600 --> 00:32:37,430
depths of what a string is right now.

401
00:32:38,330 --> 00:32:44,450
Just think that strings have a few important things you need to put double quotes around them, that's

402
00:32:44,450 --> 00:32:48,830
what makes it a string, as well as putting these string data type.

403
00:32:50,490 --> 00:32:58,350
It also needs to have this study, Colin Cullen, unless you are using namespace up here like this,

404
00:33:01,080 --> 00:33:06,450
but and then as well, you know, you may or may not need to include this at certain points.

405
00:33:07,140 --> 00:33:15,210
But if you're going to use something like this, like escort length, you definitely need to include

406
00:33:16,020 --> 00:33:16,700
string.

407
00:33:17,280 --> 00:33:23,550
But depending on your version of C++, you might be able to get away with making strings and printing

408
00:33:23,550 --> 00:33:28,590
them out with just the i o stream being included right here.

409
00:33:30,690 --> 00:33:37,300
OK, so with that, I'm going to let you go and not talk about the strings anymore.

410
00:33:37,320 --> 00:33:44,940
We will be using them, though, in the near future when we learn about loops and taking in user input.

411
00:33:45,810 --> 00:33:48,630
So with that, I will see you in the next lectures.
