1 00:00:08,000 --> 00:00:14,625 Welcome back. We can also use the accumulator pattern to accumulate strings. 2 00:00:14,625 --> 00:00:20,915 In this case, you start with an empty string and keep appending to it. 3 00:00:20,915 --> 00:00:23,480 But when you append, you'll have to reassign to 4 00:00:23,480 --> 00:00:28,520 the accumulator variable because you can't actually change the original string. 5 00:00:28,520 --> 00:00:30,785 So here's an example, 6 00:00:30,785 --> 00:00:37,635 a Michigan winter example where you've got someone who's got chattery teeth, 7 00:00:37,635 --> 00:00:39,460 because it's so cold outside, 8 00:00:39,460 --> 00:00:43,175 all of their letters are coming out double. 9 00:00:43,175 --> 00:00:45,380 So they're going to say a word like 10 00:00:45,380 --> 00:00:48,050 "Hello" and because their teeth are chattering it comes 11 00:00:48,050 --> 00:00:53,120 out as "Helloo". So here's how we do that. 12 00:00:53,120 --> 00:00:59,615 We're going to start by asking the user to type in a value that's on line one, 13 00:00:59,615 --> 00:01:08,760 they enter some text and that gets assigned to the variable s. Let's suppose that, 14 00:01:10,960 --> 00:01:16,530 let's make it easier for us, they say "cat", 15 00:01:19,000 --> 00:01:24,840 the accumulator variable is going to have our results so far, 16 00:01:25,250 --> 00:01:31,310 teeth chattering version of that string that we've accumulated so far. 17 00:01:31,310 --> 00:01:38,580 Initially, there's nothing but it's going to accumulate things, 18 00:01:38,580 --> 00:01:41,890 and then we start iterating one letter at a time, 19 00:01:41,890 --> 00:01:45,990 the variable c is going to be bound to the letter "c", 20 00:01:46,090 --> 00:01:53,095 and then we are ready to execute the accumulation part line four for the first time. 21 00:01:53,095 --> 00:02:00,525 So it's going to take our current value of ac which is an empty string, 22 00:02:00,525 --> 00:02:06,230 it's going to add the current value of c which is the letter "c", 23 00:02:06,230 --> 00:02:17,345 and then it's going to give us a dash and the letter "c" again and another dash, 24 00:02:17,345 --> 00:02:26,410 which yields a string "c-c-", 25 00:02:29,250 --> 00:02:35,730 and that becomes the new value for ac. 26 00:02:35,730 --> 00:02:40,600 The old string is still there but we don't worry about it anymore 27 00:02:40,600 --> 00:02:45,955 because ac has been reassigned to this new string cat cat. 28 00:02:45,955 --> 00:02:49,150 We're now ready to go onto the second iteration, 29 00:02:49,150 --> 00:02:57,040 where we get to line four for the second time c is now bound to the letter variable "c", 30 00:02:57,040 --> 00:03:01,970 c for character is now down to the letter "a", 31 00:03:01,970 --> 00:03:05,670 the second letter in our string, 32 00:03:05,670 --> 00:03:10,680 and we're now going to make a, 33 00:03:10,680 --> 00:03:12,240 let me write it over here, 34 00:03:12,240 --> 00:03:16,950 we're going to make a string that has the old value of 35 00:03:16,950 --> 00:03:22,485 ac and then we append the letter a and a dash, 36 00:03:22,485 --> 00:03:26,230 and a again and another dash, 37 00:03:27,260 --> 00:03:31,510 and that becomes our new value. 38 00:03:34,520 --> 00:03:40,650 Similarly for "t" we're going to extend 39 00:03:40,650 --> 00:03:48,030 this each time that 40 00:03:48,030 --> 00:03:51,915 we do all this concatenation, 41 00:03:51,915 --> 00:03:55,010 we have to re-assign to a, 42 00:03:55,010 --> 00:03:58,030 because we're not actually changing the own, 43 00:03:58,030 --> 00:04:01,285 we have to reassign to the variable ac I should have said 44 00:04:01,285 --> 00:04:05,530 and we have to do that because ac has not been changed. 45 00:04:05,530 --> 00:04:09,920 So we've made a new string and we re-assign it to ac. 46 00:04:12,930 --> 00:04:16,540 So, in summary, to accumulate a string, 47 00:04:16,540 --> 00:04:19,600 you start with an empty string, on each iteration, 48 00:04:19,600 --> 00:04:22,780 you re-assign the accumulator variable with a concatenation of 49 00:04:22,780 --> 00:04:27,965 its old value and some new stuff. 50 00:04:27,965 --> 00:04:34,020 At the end, you've accumulated something like" c-c-a-a-t-t. 51 00:04:34,040 --> 00:04:44,265 So let's run it, I enter cat, I get c-c-a-a-t-t, 52 00:04:44,265 --> 00:04:53,445 Michigan frozen winter speaking picking cat or if I run it and ask for dog, 53 00:04:53,445 --> 00:05:00,580 I'll get d-d-o-o-g-g-. See you next time.