Hello again! In this video, we're going to look into searching strings.
The library string class has a member function called find. We can call this on a string, and it will look for
the first occurrence of the argument in that string.
So if we have some string object, then we call find, the argument to this could be a library string,
of course.
It can also be a single character, or it can be a C string.
And then the function will perform a case-sensitive search, so if you try to find the letter capital
'O', that won't succeed because we only have lower case 'o'.
If the call succeeds, it returns the index of the first matching character.
So for lower case 'o', that will be four. The index of that letter 'o'. For the argument "or" that will be
seven, which is where the string appears, the C string.
If the search does not succeed, it returns a special value, which is string double colon npos, and
this represents an impossible index.
So we've coded this up. So we have our string, "Hello world", and then we have the find operations.
We check the return value.
If the return value is not equal to string::npos, then we have a valid index and it has found something.
And we print out the index.
We're using raw strings, so we do not need to put back slashes inside the string.
And if it is equal to npos, then we did not find it.
So there's the single character, the C string, and then the search for capital 'O'.
So there we are, the first occurrence of lowercase o is at index four. The string "or" is seven.
And we could not find capital 'O' in that string.
So we can actually use the returned value to index into the string and access the characters or modify
them.
But we need to make sure that we are actually using a valid index.
So we should always check the index before actually using it.
So here is that code, we're going to look for the first sequence of the letter 'o' again, and this time
we're going to change it to 'p'.
So we had "Hello world" and now we have "Hellp world"!
We can also search for the last occurrence of a character. So there is rfind, which is a reverse
find.
So this will start at the end of the string and it will go back until it finds an occurrence.
So if we do rfind of 'o', this will return the index of this letter 'o'. And if we do "or", that is
going to be the same return value.
So by default, this will start from the end of the string, the last character. We can change this
by passing another argument.
So if we have the argument five, that means it is going to start from the element with index
five, which is this space character.
So it's going to find the last occurrence of 'o' before the space.
So here is the same program we had before, but now we are using rfind instead of find, and we are
going to change the last matching character.
So now it is
"Hello wprld"!
So that's searching for a single character or a single string.
We can also search for one of a group of characters.
To do that, we use the member function find_first_of.
So we give this a string.
The string contains the letters that we are looking for.
So we are looking for one of "aeiou" in the "Hello world" string.
So this is going to be turned the first of these, which is going to be the 'e' from "hello".
There is also a find_last_of, which will return the index of the last occurrence of any of these characters.
So that's going to be the 'o' in "world".
And you can also invert this. So you can find the first character that is not a vowel.
So that is going to be the 'H' of "hello".
And you can find the last character that is not one of these.
So that's going to be the 'd' in "world".
So here we are, we are just going to find the first and last voewl and non-vowel. So the first vowel
is at index one. The last one is at index seven.
The first non-vowel is index zero, which is 'H', and the last non-vowel is index
10, which is 'd'.
So if you want a little exercise, you can change this program.
So it actually prints out these characters.
Okay, so that's it for this video.
I'll see you next time.
But meanwhile, keep coding!