WEBVTT

00:01.380 --> 00:05.160
Hello again! In this video, we're going to look into searching strings.

00:07.250 --> 00:13.460
The library string class has a member function called find. We can call this on a string, and it will look for

00:13.460 --> 00:16.160
the first occurrence of the argument in that string.

00:17.270 --> 00:23.020
So if we have some string object, then we call find, the argument to this could be a library string,

00:23.030 --> 00:23.480
of course.

00:23.900 --> 00:27.950
It can also be a single character, or it can be a C string.

00:30.170 --> 00:36.670
And then the function will perform a case-sensitive search, so if you try to find the letter capital

00:36.680 --> 00:39.860
'O', that won't succeed because we only have lower case 'o'.

00:39.860 --> 00:45.830
If the call succeeds, it returns the index of the first matching character.

00:46.460 --> 00:54.320
So for lower case 'o', that will be four. The index of that letter 'o'. For the argument "or" that will be

00:54.320 --> 00:59.510
seven, which is where the string appears, the C string.

01:01.280 --> 01:08.390
If the search does not succeed, it returns a special value, which is string double colon npos, and

01:08.390 --> 01:10.520
this represents an impossible index.

01:14.430 --> 01:20.670
So we've coded this up. So we have our string, "Hello world", and then we have the find operations.

01:21.690 --> 01:23.280
We check the return value.

01:23.910 --> 01:30.150
If the return value is not equal to string::npos, then we have a valid index and it has found something.

01:30.870 --> 01:32.190
And we print out the index.

01:32.550 --> 01:37.350
We're using raw strings, so we do not need to put back slashes inside the string.

01:38.880 --> 01:42.600
And if it is equal to npos, then we did not find it.

01:44.780 --> 01:50.730
So there's the single character, the C string, and then the search for capital 'O'.

01:53.900 --> 02:00.830
So there we are, the first occurrence of lowercase o is at index four. The string "or" is seven.

02:01.100 --> 02:04.040
And we could not find capital 'O' in that string.

02:09.420 --> 02:15.570
So we can actually use the returned value to index into the string and access the characters or modify

02:15.570 --> 02:15.810
them.

02:16.260 --> 02:20.490
But we need to make sure that we are actually using a valid index.

02:21.120 --> 02:24.810
So we should always check the index before actually using it.

02:28.260 --> 02:32.820
So here is that code, we're going to look for the first sequence of the letter 'o' again, and this time

02:32.820 --> 02:34.350
we're going to change it to 'p'.

02:36.570 --> 02:39.930
So we had "Hello world" and now we have "Hellp world"!

02:42.690 --> 02:48.420
We can also search for the last occurrence of a character. So there is rfind, which is a reverse

02:48.420 --> 02:48.900
find.

02:49.440 --> 02:54.730
So this will start at the end of the string and it will go back until it finds an occurrence.

02:54.750 --> 03:03.240
So if we do rfind of 'o', this will return the index of this letter 'o'. And if we do "or", that is

03:03.240 --> 03:04.680
going to be the same return value.

03:07.580 --> 03:12.830
So by default, this will start from the end of the string, the last character. We can change this

03:12.830 --> 03:14.260
by passing another argument.

03:14.280 --> 03:18.890
So if we have the argument five, that means it is going to start from the element with index

03:18.890 --> 03:22.520
five, which is this space character.

03:23.300 --> 03:26.150
So it's going to find the last occurrence of 'o' before the space.

03:28.630 --> 03:33.280
So here is the same program we had before, but now we are using rfind instead of find, and we are

03:33.280 --> 03:37.060
going to change the last matching character.

03:37.180 --> 03:38.290
So now it is 

03:38.620 --> 03:39.070
"Hello wprld"!

03:42.830 --> 03:46.730
So that's searching for a single character or a single string.

03:47.330 --> 03:51.560
We can also search for one of a group of characters.

03:53.070 --> 03:56.230
To do that, we use the member function find_first_of.

03:56.880 --> 03:58.170
So we give this a string.

03:58.650 --> 04:02.020
The string contains the letters that we are looking for.

04:02.040 --> 04:07.080
So we are looking for one of "aeiou" in the "Hello world" string.

04:08.340 --> 04:12.390
So this is going to be turned the first of these, which is going to be the 'e' from "hello".

04:13.530 --> 04:19.470
There is also a find_last_of, which will return the index of the last occurrence of any of these characters.

04:20.160 --> 04:21.900
So that's going to be the 'o' in "world".

04:23.600 --> 04:28.580
And you can also invert this. So you can find the first character that is not a vowel.

04:29.330 --> 04:31.310
So that is going to be the 'H' of "hello".

04:32.210 --> 04:35.090
And you can find the last character that is not one of these.

04:35.810 --> 04:37.930
So that's going to be the 'd' in "world".

04:40.580 --> 04:48.010
So here we are, we are just going to find the first and last voewl and non-vowel. So the first vowel

04:48.010 --> 04:50.840
is at index one. The last one is at index seven.

04:51.560 --> 04:56.800
The first non-vowel is index zero, which is 'H', and the last non-vowel is index

04:56.810 --> 04:58.100
10, which is 'd'.

04:58.880 --> 05:01.550
So if you want a little exercise, you can change this program.

05:01.550 --> 05:03.950
So it actually prints out these characters.

05:05.270 --> 05:07.280
Okay, so that's it for this video.

05:07.640 --> 05:08.480
I'll see you next time.

05:08.480 --> 05:10.730
But meanwhile, keep coding!
