1 00:00:05,440 --> 00:00:08,550 In this video, I'd like to go over using the c++ string 2 00:00:08,559 --> 00:00:10,120 class and some examples there. 3 00:00:10,559 --> 00:00:15,049 So I'm in the section 10 workspace in the CPP strings project. 4 00:00:16,230 --> 00:00:19,080 Now remember, the string class is a class. 5 00:00:19,080 --> 00:00:22,300 So when we create strings in this case, here you see I'm creating 6 00:00:22,570 --> 00:00:27,820 eight strings from S0 to S7 on lines 9 to 16, these are objects. 7 00:00:28,080 --> 00:00:31,720 We'll talk about exactly what objects are in the world of object-oriented 8 00:00:31,720 --> 00:00:33,320 programming in a future video. 9 00:00:33,650 --> 00:00:37,380 But we are using object-oriented programming here to work with 10 00:00:37,400 --> 00:00:39,280 these c++ string objects. 11 00:00:39,470 --> 00:00:42,440 Okay, it's important that you include the string library. 12 00:00:43,130 --> 00:00:45,780 Otherwise, you'll get a lot of compiler errors, and I'm 13 00:00:45,780 --> 00:00:47,550 using the standard name space. 14 00:00:47,830 --> 00:00:50,419 First thing is let's declare and initialize some 15 00:00:50,420 --> 00:00:51,679 of these string objects. 16 00:00:52,049 --> 00:00:54,185 Here, I'm declaring S0 through S7. 17 00:00:54,440 --> 00:00:56,989 You can see S0 -- I'm just saying string S0. 18 00:00:57,580 --> 00:01:01,910 It's important to understand that with classes and objects 19 00:01:01,910 --> 00:01:04,330 in this case, S0 is an object. 20 00:01:04,730 --> 00:01:06,640 I am getting an initialized object. 21 00:01:06,690 --> 00:01:08,650 There's not going to be any garbage in there. 22 00:01:08,650 --> 00:01:10,592 And we'll display that in a second just so you can 23 00:01:10,690 --> 00:01:11,729 see what that looks like. 24 00:01:12,410 --> 00:01:14,810 Then I'm declaring other string objects here. 25 00:01:14,810 --> 00:01:17,479 So S1 is being initialized to apple. 26 00:01:17,780 --> 00:01:18,650 S2 to banana. 27 00:01:19,010 --> 00:01:19,729 S3 to kiwi. 28 00:01:19,730 --> 00:01:21,220 S4 to apple and so forth. 29 00:01:21,220 --> 00:01:23,460 S5 is being initialized to S1. 30 00:01:23,740 --> 00:01:25,070 Well, S1 is apple. 31 00:01:25,289 --> 00:01:30,130 So a copy of that string is going to be put into S5. 32 00:01:30,130 --> 00:01:32,590 So S5 is going to be its own version of apple. 33 00:01:32,820 --> 00:01:35,860 So if I change S1 it's not going to affect S5. 34 00:01:36,670 --> 00:01:39,140 There's also a couple of other initializers. 35 00:01:39,140 --> 00:01:41,940 There's actually a bunch of initializers and constructor 36 00:01:41,950 --> 00:01:45,270 style initializers, a whole bunch of examples that you can use, 37 00:01:45,270 --> 00:01:48,890 but I'm just sticking to the the commonly used ones in modern c++. 38 00:01:49,110 --> 00:01:51,730 In this case, S6 is S1. 39 00:01:51,780 --> 00:01:55,060 I'm initializing it from S1 which is apple, but I'm 40 00:01:55,060 --> 00:01:56,380 only using a part of it. 41 00:01:56,530 --> 00:01:59,380 I'm starting at index 0, and I want three characters. 42 00:01:59,599 --> 00:02:02,280 So it's only going to be initialized to the string app. 43 00:02:02,670 --> 00:02:06,309 And then in this case S7, I'm initializing that 44 00:02:06,310 --> 00:02:08,350 string to 10 x characters. 45 00:02:08,350 --> 00:02:11,420 Notice the x is in single quotes here, so that's a character. 46 00:02:12,050 --> 00:02:15,769 And also notice that I'm using parentheses here, and not curlies. 47 00:02:16,000 --> 00:02:19,070 This is a constructor style initialization, and that's the 48 00:02:19,070 --> 00:02:20,539 one that works with this example. 49 00:02:20,929 --> 00:02:23,949 Before we look at the initialization values, let's look at this right here. 50 00:02:24,160 --> 00:02:28,010 Notice that I declared that string object S0. 51 00:02:28,370 --> 00:02:32,186 If this had been what we the example that we saw using c style strings, 52 00:02:32,186 --> 00:02:34,980 and we never initialized it you remember we saw that garbage, 53 00:02:35,139 --> 00:02:36,320 you're not going to get that here. 54 00:02:36,509 --> 00:02:39,909 So if I display S0, you won't see anything because it's empty. 55 00:02:40,379 --> 00:02:45,110 And if I ask S0, what's your length, this is a method, this is an object 56 00:02:45,110 --> 00:02:47,400 and this is what object-oriented programming is all about. 57 00:02:47,730 --> 00:02:50,420 I'm asking the S0 object. 58 00:02:50,440 --> 00:02:53,220 In other words, this string right here, hey, what's your length? 59 00:02:53,679 --> 00:02:55,079 And it's going to return its length. 60 00:02:55,700 --> 00:02:58,670 In this case, I expect back a 0 because it's an empty string. 61 00:02:58,910 --> 00:02:59,910 So let me run this. 62 00:03:00,480 --> 00:03:03,859 And you can see that nothing printed out because the string is empty. 63 00:03:04,060 --> 00:03:07,480 And I get back a 0, which says that the string has no length, it's empty. 64 00:03:08,040 --> 00:03:10,160 So you can see that it is being initialized. 65 00:03:10,170 --> 00:03:11,820 That's really important with objects. 66 00:03:12,460 --> 00:03:16,160 Okay, I'm going to comment that out, and then let's look at some 67 00:03:16,160 --> 00:03:17,750 of these initialization examples. 68 00:03:18,750 --> 00:03:21,859 I've just initialized those objects right up here where I declared them. 69 00:03:22,130 --> 00:03:23,600 Now I'm just going to display them. 70 00:03:23,760 --> 00:03:28,290 And you can see that we're getting exactly what we expected here, let 71 00:03:28,290 --> 00:03:29,670 me scroll that up just a little bit. 72 00:03:29,900 --> 00:03:30,989 So S1 is apple. 73 00:03:30,989 --> 00:03:32,919 S2 is banana, then I've got kiwi. 74 00:03:33,230 --> 00:03:37,480 Apple with a lowercase a, apple and app that's that three. 75 00:03:38,250 --> 00:03:41,410 The initializer where we only wanted the first three characters. 76 00:03:41,740 --> 00:03:43,390 And then I've got 10 xs. 77 00:03:43,700 --> 00:03:46,040 Okay, so you can see really powerful, really easy way to 78 00:03:46,760 --> 00:03:48,100 initialize these string objects. 79 00:03:48,509 --> 00:03:53,560 So now let's comment this out again and work with some of the operators 80 00:03:53,560 --> 00:03:55,200 and methods of the string class. 81 00:03:55,790 --> 00:03:57,970 In this case, I'm comparing. 82 00:03:58,540 --> 00:04:03,319 If you recall using c style strings, we had to use the strcmp or the string 83 00:04:03,320 --> 00:04:08,129 compare function pass in two strings and get back either 0 or a positive 84 00:04:08,130 --> 00:04:11,100 value or a negative value and then we'd have to check things like that. 85 00:04:11,129 --> 00:04:14,100 Well, here we can just use the operators that we're used to. 86 00:04:14,660 --> 00:04:17,969 And I'm using the boolalpha io manipulator here, just so it 87 00:04:17,970 --> 00:04:20,140 displays the words true and false. 88 00:04:20,269 --> 00:04:22,999 And you can see I've got a whole bunch of examples, and it's actually easier 89 00:04:23,000 --> 00:04:24,840 to run it than to read all that code. 90 00:04:25,139 --> 00:04:29,060 But you can see here that I'm comparing apple with 91 00:04:29,060 --> 00:04:30,430 apple, and I get back true. 92 00:04:30,980 --> 00:04:33,630 I'm comparing apple with banana, I get back false. 93 00:04:33,920 --> 00:04:36,980 Then I'm asking is apple not equal to banana, that's true 94 00:04:36,980 --> 00:04:38,030 because they're not equal. 95 00:04:38,570 --> 00:04:43,270 Is apple less than banana, true because the capital a comes before 96 00:04:43,270 --> 00:04:46,109 the capital b in the ascii table. 97 00:04:46,809 --> 00:04:49,060 What about banana greater than apple, yes. 98 00:04:49,070 --> 00:04:51,770 Again I just flipped the sign and flipped the strings. 99 00:04:52,220 --> 00:04:55,010 What about apple with a lowercase a, is that less than 100 00:04:55,050 --> 00:04:56,700 apple with an uppercase a? 101 00:04:57,030 --> 00:05:00,630 It is not because the uppercase characters come before the lowercase 102 00:05:00,630 --> 00:05:02,390 characters in the ascii table. 103 00:05:02,839 --> 00:05:05,329 And then the last one I'm using is right here where I'm actually 104 00:05:05,330 --> 00:05:10,609 comparing an object S1 with a c style string literal apple. 105 00:05:11,000 --> 00:05:14,440 And that works perfectly fine because what happens is this string 106 00:05:14,450 --> 00:05:17,459 literal right here gets converted to an object and then we're doing 107 00:05:17,559 --> 00:05:19,050 object to object comparison. 108 00:05:19,290 --> 00:05:20,440 So that's true. 109 00:05:21,020 --> 00:05:23,249 Remember, it's doing character by character comparison. 110 00:05:24,090 --> 00:05:27,760 Okay, so real familiar, real intuitive and very easy to use. 111 00:05:28,160 --> 00:05:31,490 So let me comment that out, and let's do assignment. 112 00:05:32,240 --> 00:05:36,780 Again, if you recall with the seesaw strings we had to do strcpy 113 00:05:36,820 --> 00:05:41,680 or string copy, using c++ string objects, we just use the assignment 114 00:05:41,680 --> 00:05:43,020 operator, just like we're used to. 115 00:05:43,320 --> 00:05:45,890 So they feel like they're really part of the language, just like 116 00:05:46,030 --> 00:05:48,720 you would with integers and doubles and characters and so forth. 117 00:05:49,010 --> 00:05:52,499 So in this case, I'm assigning watermelon to S1. 118 00:05:53,569 --> 00:05:56,350 Okay, whatever was in S1 before is now gone. 119 00:05:56,549 --> 00:05:58,619 Okay, I've overwritten it with watermelon. 120 00:05:59,160 --> 00:06:01,570 And then I'm outputting S1 is now. 121 00:06:01,570 --> 00:06:03,490 And obviously, I expect back watermelon. 122 00:06:04,020 --> 00:06:09,489 When I say assign S1 while S1 is watermelon to S2, I'm going to get a 123 00:06:09,490 --> 00:06:11,610 copy of that watermelon object, right. 124 00:06:11,660 --> 00:06:14,489 So they're not pointing to the same watermelon. 125 00:06:14,860 --> 00:06:17,039 I'm going to get two copies that are individual. 126 00:06:17,039 --> 00:06:20,489 So if I change S1, it won't affect S2 and vice versa. 127 00:06:21,150 --> 00:06:24,330 And if I display S2, I also expect watermelon here. 128 00:06:24,850 --> 00:06:27,550 Okay, here, I'm saying S3 is Frank. 129 00:06:27,550 --> 00:06:31,880 So I'm assigning the string frank to S3 and that should also display Frank. 130 00:06:32,770 --> 00:06:35,610 Now once I've assigned these strings, I can manipulate them. 131 00:06:35,900 --> 00:06:40,680 So in this case, I can say change the zeroth character in S3. 132 00:06:40,680 --> 00:06:42,980 Well, that's the F in Frank right here. 133 00:06:43,720 --> 00:06:47,269 Change that to a capital C, and notice single quotes because 134 00:06:47,270 --> 00:06:48,390 I'm changing a character. 135 00:06:48,639 --> 00:06:53,850 So I'm saying assign the capital C to the first element of that string, 136 00:06:53,850 --> 00:06:55,500 right, the one at the 0 index. 137 00:06:56,080 --> 00:07:00,140 So this would change it to crank, which will print out crank. 138 00:07:00,950 --> 00:07:02,849 I can also use the at method. 139 00:07:03,330 --> 00:07:05,949 Again, this is the whole idea of object-oriented programming. 140 00:07:05,950 --> 00:07:10,149 I'm saying, hey S3 change the character at 141 00:07:10,150 --> 00:07:12,409 position 0 to a capital p. 142 00:07:12,890 --> 00:07:15,350 So when I display that, it should display prank. 143 00:07:15,660 --> 00:07:18,989 Let's run that and double check, so you can see here 144 00:07:18,990 --> 00:07:21,200 that S1 is watermelon. 145 00:07:21,200 --> 00:07:22,439 S2 is watermelon. 146 00:07:22,510 --> 00:07:23,520 S3 is frank. 147 00:07:23,520 --> 00:07:28,610 And then I'm changing the first letter of Frank to c and displaying Crank. 148 00:07:29,029 --> 00:07:31,460 And then I'm changing it to a p and displaying Prank. 149 00:07:32,550 --> 00:07:34,960 All right, I'll comment this, out and let's look at concatenation. 150 00:07:38,040 --> 00:07:42,639 Again, I'll compare this to the c style string where we had to use 151 00:07:42,639 --> 00:07:46,650 the string cat function strcat to concatenate two strings. 152 00:07:46,900 --> 00:07:50,290 Here, we could just simply use the plus operator. 153 00:07:50,680 --> 00:07:54,800 And in this case, I'm saying assign watermelon to S3. 154 00:07:55,320 --> 00:07:56,470 Okay, that's easy. 155 00:07:56,590 --> 00:07:59,539 But now I'm saying S3 equals S5. 156 00:08:00,750 --> 00:08:07,880 Well, S5 is apple, and S2 is banana and juice. 157 00:08:07,880 --> 00:08:14,420 I'm concatenating these 1, 2, 3, 4 strings together by 158 00:08:14,420 --> 00:08:16,420 simply using the plus operator. 159 00:08:16,980 --> 00:08:20,979 The plus operator is considered overloaded, that means it works with 160 00:08:21,009 --> 00:08:22,510 integers, it works with doubles. 161 00:08:22,690 --> 00:08:24,900 And in this case, it works with string objects. 162 00:08:25,780 --> 00:08:28,289 So what we're going to do here is we're going to say apple and 163 00:08:28,290 --> 00:08:30,530 banana juice and assign that to S3. 164 00:08:30,750 --> 00:08:32,630 So in this case, watermelon is gone. 165 00:08:32,669 --> 00:08:33,730 We're overriding that. 166 00:08:34,530 --> 00:08:38,270 And then when we display S3, it should say apple and banana juice, right. 167 00:08:38,270 --> 00:08:39,570 So let me run that quickly. 168 00:08:40,770 --> 00:08:41,489 And there you go. 169 00:08:41,489 --> 00:08:44,420 S3 is now apple and banana juice just like we expect. 170 00:08:44,960 --> 00:08:48,430 If you try something like this, you're going to get a compiler error. 171 00:08:48,580 --> 00:08:52,939 The problem here is that we've got a c style string literal 172 00:08:53,309 --> 00:08:57,180 and another c style string literal and the plus operator. 173 00:08:57,880 --> 00:09:00,599 C style strings don't work with the plus operator. 174 00:09:00,600 --> 00:09:03,850 If you wanted to do something like this, you would have to scrcat 175 00:09:03,850 --> 00:09:05,770 and concatenate them together. 176 00:09:06,280 --> 00:09:09,170 In this case, you're going to get a compiler error saying 177 00:09:09,170 --> 00:09:12,440 something about I don't know what to do with these two character 178 00:09:12,450 --> 00:09:14,190 arrays and this binary operator. 179 00:09:15,210 --> 00:09:18,820 Okay, so keep that in mind if you're trying to use the plus 180 00:09:18,820 --> 00:09:21,510 operator, you've got to use it on at least one of the objects. 181 00:09:21,510 --> 00:09:25,439 Notice over here, we're using an object and a c style string literal. 182 00:09:25,450 --> 00:09:27,069 That's okay, because it'll convert. 183 00:09:27,360 --> 00:09:30,010 But when you've got two c style string literals like this, 184 00:09:30,020 --> 00:09:31,150 it doesn't know what to do. 185 00:09:31,950 --> 00:09:34,670 Let's move on and talk about looping. 186 00:09:35,190 --> 00:09:40,060 In this case, we're going to use a for loop to loop through a string. 187 00:09:40,770 --> 00:09:43,310 Remember, a string is simply a collection, right, a bunch 188 00:09:43,310 --> 00:09:45,799 of characters so we can easily loop through them. 189 00:09:45,980 --> 00:09:47,539 And we'll loop through them two different ways. 190 00:09:47,550 --> 00:09:49,860 First, we'll use a regular for loop, and then we'll use 191 00:09:49,860 --> 00:09:51,070 the range-based for loop. 192 00:09:51,750 --> 00:09:55,660 In this case, I'm assigning apple to S1, just so we have 193 00:09:55,660 --> 00:09:56,930 something to start with right here. 194 00:09:57,420 --> 00:09:59,460 And I'm initializing I to 0. 195 00:09:59,500 --> 00:10:01,950 Notice that the type of I is size T. 196 00:10:02,570 --> 00:10:04,590 This is really the proper way to do this. 197 00:10:04,650 --> 00:10:07,900 It's always going to be a non-negative number, right because 198 00:10:07,900 --> 00:10:09,479 we're looping through a string. 199 00:10:09,790 --> 00:10:12,800 Now if you've got looping variables that need to go negative, you 200 00:10:12,800 --> 00:10:15,510 don't want to use size T, you just want to use a regular integer 201 00:10:15,590 --> 00:10:16,740 or something that's signed. 202 00:10:17,060 --> 00:10:21,090 But in these kinds of examples, size T is the proper type to use. 203 00:10:21,370 --> 00:10:25,220 So I'm initializing I to 0 and I'm going to loop while I is 204 00:10:25,220 --> 00:10:26,710 less than the string's length. 205 00:10:26,950 --> 00:10:29,719 Remember, we're going from 0 to the length minus one. 206 00:10:30,029 --> 00:10:33,980 So in this case, I'm asking the S1 object, what's your length, and 207 00:10:33,980 --> 00:10:35,829 it's returning 5 because it's apple. 208 00:10:36,610 --> 00:10:39,800 So I'm going to loop 0, 1, 2, 3, 4 and then I'm going to stop. 209 00:10:39,870 --> 00:10:42,179 And as you see I'm incrementing one each time. 210 00:10:42,910 --> 00:10:45,620 So every time through the loop, all I'm doing is just simply outputting 211 00:10:46,199 --> 00:10:49,930 that character at the ith location. 212 00:10:50,640 --> 00:10:53,790 So cout S1.ati. 213 00:10:53,790 --> 00:10:57,630 I could use array subscript syntax. 214 00:10:57,670 --> 00:10:58,590 They both work. 215 00:10:59,000 --> 00:11:02,980 The .at method does bounce checking, the subscript method does not. 216 00:11:03,520 --> 00:11:06,980 So when I run this, I should see apple print out across the screen. 217 00:11:08,030 --> 00:11:08,770 And there it is. 218 00:11:08,800 --> 00:11:11,100 I'm just displaying it one letter at a time. 219 00:11:11,920 --> 00:11:16,029 I can also do this using the range-based for loop in which 220 00:11:16,030 --> 00:11:21,519 case I'm just saying for every character cin S1 output it, right. 221 00:11:21,690 --> 00:11:23,980 So it's going to go a p p l e, stop. 222 00:11:24,480 --> 00:11:26,920 So in this case, I should have a c apple twice now, right. 223 00:11:26,960 --> 00:11:29,470 Once for the for loop and once for the range based for loop. 224 00:11:30,300 --> 00:11:33,920 You can see some of these are very, very intuitive and easy to use. 225 00:11:34,010 --> 00:11:34,800 There's a couple left. 226 00:11:34,830 --> 00:11:36,429 Let's talk about substring here. 227 00:11:37,250 --> 00:11:39,000 This was, again, is very intuitive. 228 00:11:39,450 --> 00:11:42,579 Suppose S1 is -- this is a test. 229 00:11:42,580 --> 00:11:44,830 I've just assigned that string to S1. 230 00:11:45,420 --> 00:11:49,370 And I want the substring that starts at 0 and it's 4 long. 231 00:11:49,380 --> 00:11:53,170 So it's going to go t h i s, right. 232 00:11:53,200 --> 00:11:58,200 The capital t is index 0, so that's going to return the 233 00:11:58,299 --> 00:12:00,559 word this or the string this. 234 00:12:01,610 --> 00:12:04,420 In the second substring example, I'm starting at 235 00:12:04,420 --> 00:12:09,110 index 5, which is 0 1 2 3 4 5. 236 00:12:09,110 --> 00:12:11,350 So it's starting at the I and is. 237 00:12:11,730 --> 00:12:15,930 And I want two characters, so it's grabbing this is, right there. 238 00:12:16,460 --> 00:12:17,700 And that's what it's returning. 239 00:12:18,150 --> 00:12:21,360 And then the last one, I'm starting at index 10, which is the lowercase 240 00:12:21,940 --> 00:12:25,890 t and test, and I just want 4 back, and it's giving me test. 241 00:12:26,139 --> 00:12:28,209 So you can see, it's very, very easy. 242 00:12:28,750 --> 00:12:32,600 And of course all these you can see here it's displaying this is a test. 243 00:12:33,059 --> 00:12:36,889 All these are hard coded but you can certainly use variables for these 244 00:12:36,889 --> 00:12:40,810 integers and loop through and loops and grab pieces of substrings, and 245 00:12:41,030 --> 00:12:43,109 you can do a lot of really cool stuff. 246 00:12:43,830 --> 00:12:45,540 We can erase characters. 247 00:12:45,820 --> 00:12:48,040 In this example, I'm saying S1. 248 00:12:48,040 --> 00:12:50,150 Again, I'm assigning this is a test to it. 249 00:12:50,740 --> 00:12:53,329 And I'm erasing from S1. 250 00:12:53,359 --> 00:12:55,790 Again, notice the object-oriented style. 251 00:12:56,089 --> 00:12:58,720 Think of this almost as you're asking 252 00:12:58,720 --> 00:13:02,420 the S1 object, right because notice it's S1. 253 00:13:02,420 --> 00:13:07,920 So you're saying the erase method is going to execute on that object S1 254 00:13:07,920 --> 00:13:12,040 because we could have S2.erase S8.erase, anything.erase, right. 255 00:13:12,040 --> 00:13:15,540 But in this case, it's happening on S1, which is 256 00:13:15,540 --> 00:13:16,880 this is a test right here. 257 00:13:17,370 --> 00:13:22,890 So what I'm saying here is erase starting at 0 and erase 5 characters. 258 00:13:23,360 --> 00:13:27,940 So it's going to erase the capital t, the h, the i, the s and the space. 259 00:13:29,180 --> 00:13:34,070 When I display that, S1 is now just the is a test part. 260 00:13:34,120 --> 00:13:36,860 I got rid of the this and the space from the beginning. 261 00:13:38,690 --> 00:13:40,270 All right, let's do a couple of more. 262 00:13:40,500 --> 00:13:43,374 The getline is a little bit different from the getline 263 00:13:43,650 --> 00:13:45,120 that we saw with c strings. 264 00:13:45,360 --> 00:13:50,000 If you remember, with c strings we said cin.getline, and we had a length. 265 00:13:50,270 --> 00:13:53,790 Well, we're not worried about a length here because the c++ string 266 00:13:53,990 --> 00:13:55,760 class handles lengths for us. 267 00:13:55,990 --> 00:13:59,010 So it looks a little different, but it's the same basic effect. 268 00:13:59,469 --> 00:14:02,220 Here, I've got a new string that I'm declaring called full name 269 00:14:02,220 --> 00:14:03,810 and I'm initializing it to empty. 270 00:14:04,150 --> 00:14:07,300 I'm asking the user to enter their full name, and then I'm 271 00:14:07,300 --> 00:14:08,960 using the getline function here. 272 00:14:09,160 --> 00:14:12,530 And I'm passing in where I want to get the input from, in 273 00:14:12,530 --> 00:14:14,040 this case the keyboard or cin. 274 00:14:14,040 --> 00:14:17,159 And where I want to store the result, full name. 275 00:14:18,300 --> 00:14:22,870 This will get an entire line up until I press enter, and 276 00:14:23,639 --> 00:14:24,760 then I just display the name. 277 00:14:24,760 --> 00:14:28,020 So in this case, if I run this and type my name, 278 00:14:30,420 --> 00:14:32,340 notice full name is 279 00:14:32,340 --> 00:14:36,769 Frank Metropolous, my entire name, it doesn't stop at the space the same 280 00:14:36,770 --> 00:14:39,239 way that the extraction operator does. 281 00:14:39,889 --> 00:14:42,950 The last thing we'll do is we'll look at the find method, 282 00:14:43,139 --> 00:14:44,580 which is a real handy method. 283 00:14:45,510 --> 00:14:48,130 So in this case, here's the secret word. 284 00:14:48,220 --> 00:14:50,460 The secret word is boo, that's the string. 285 00:14:51,240 --> 00:14:54,910 And I've got a word variable here, which is a string object. 286 00:14:55,570 --> 00:14:58,139 I'm asking the user to enter the word to find. 287 00:14:58,150 --> 00:15:01,020 The idea is they're going to type one of these words in here, right. 288 00:15:01,390 --> 00:15:04,090 And I'm going to tell them where that word is in the string, 289 00:15:04,090 --> 00:15:05,720 what position it begins at. 290 00:15:06,150 --> 00:15:10,520 So if I typed in the, I'm saying hey S1. 291 00:15:10,860 --> 00:15:14,850 Again, the S1 object, find that word within you. 292 00:15:15,360 --> 00:15:18,349 So it's going to find the word the within it, and it's going 293 00:15:18,350 --> 00:15:22,340 to return 0 because it starts at the 0 index, right there. 294 00:15:22,570 --> 00:15:23,690 That's the t. 295 00:15:23,760 --> 00:15:28,100 Again, I'm using a size t for the type because it's always going to 296 00:15:28,100 --> 00:15:30,720 be like 0 or greater or an error. 297 00:15:30,720 --> 00:15:32,300 And I'll talk about the error in a second. 298 00:15:32,630 --> 00:15:35,990 So at this point, I'm looking for that word in the string 299 00:15:36,469 --> 00:15:40,020 and position holds its position whether it was found or not. 300 00:15:40,250 --> 00:15:43,900 How do I know whether it was found or not, I need to see if the position 301 00:15:43,900 --> 00:15:48,200 returned was that value, right, string end position, no position. 302 00:15:48,520 --> 00:15:51,329 So in this case, if the position I got back is not 303 00:15:51,330 --> 00:15:53,179 equal to that, then I found it. 304 00:15:53,650 --> 00:15:56,800 So I'm simply going to disability found word at position whatever. 305 00:15:57,120 --> 00:15:58,570 Otherwise, it wasn't found. 306 00:15:58,720 --> 00:15:59,620 So let's run this. 307 00:15:59,630 --> 00:16:01,880 And remember, the string is the secret word is boo. 308 00:16:02,370 --> 00:16:04,470 So I'm going to run this and and search for boo. 309 00:16:06,259 --> 00:16:08,690 And it says I found it at position 19. 310 00:16:08,950 --> 00:16:11,759 Let's run it again, and we'll use the word secret this time. 311 00:16:13,059 --> 00:16:16,660 It found it at position 4, and you can see there it is, 312 00:16:17,040 --> 00:16:18,760 right there at position 4. 313 00:16:19,140 --> 00:16:21,650 And we could try to find something that's not there like hello. 314 00:16:22,740 --> 00:16:24,540 And it says hello not found. 315 00:16:25,490 --> 00:16:27,840 Okay, so let me wrap up this video now. 316 00:16:28,510 --> 00:16:32,380 You have all the elements you need for the challenge that's coming up next. 317 00:16:32,720 --> 00:16:35,349 You could certainly try the challenge using c style 318 00:16:35,350 --> 00:16:37,310 strings and then c++ strings. 319 00:16:37,500 --> 00:16:40,779 But I'm just going to do it using c++ strings because we really should 320 00:16:40,790 --> 00:16:42,960 be using c++ strings going forward. 321 00:16:43,290 --> 00:16:45,199 All right, So head off to the challenge.