WEBVTT 00:01.020 --> 00:01.960 Welcome back. 00:01.980 --> 00:07.360 In Texas, editors, usually there is a feature called The Word Wrapping in this lecture. 00:07.380 --> 00:10.200 I'm going to show you how to implement the same feature. 00:10.470 --> 00:11.280 Let's get started. 00:11.910 --> 00:16.800 Now I'm going to say the Turkish translation of the opening lines of a sci fi book, The Hitchhiker's 00:16.800 --> 00:21.990 Guide to the Galaxy, into a constant laakso never printed using a full range loop like. 00:23.380 --> 00:29.530 Remember, a full range look jumps over the roofs of the Gibbons' Trink, so each loop step returns 00:29.530 --> 00:30.320 the next room. 00:31.120 --> 00:34.680 OK, let's say I want to wrap my 40 characters per line. 00:35.170 --> 00:37.830 So I'm going to declare a new constant here like so. 00:38.320 --> 00:40.510 And I'm also going to declare a new variable here. 00:41.230 --> 00:47.800 LW is short for line with by the way, you want to program in your please get used to names with fewer 00:47.800 --> 00:48.480 letters like. 00:48.490 --> 00:51.630 So this variable is the heart of this program. 00:51.670 --> 00:54.450 It keeps track of how long the current line is. 00:54.520 --> 00:56.250 I will use it to wrap the lines. 00:56.620 --> 00:58.360 This type An if statement here. 00:59.020 --> 01:04.030 This will deduct whenever the current line gets greater than the maximum allowed line with. 01:04.300 --> 01:06.220 If so, then I'll print a new line. 01:07.440 --> 01:13.920 Doing so will cut the current line, the rest of the rules will be printed to the next line after printing 01:13.920 --> 01:15.030 a room in a line. 01:15.060 --> 01:16.620 It makes the line longer. 01:16.990 --> 01:21.060 So I also need to increase the Al W.O. variable Lexar. 01:23.050 --> 01:26.840 As you can see, it has worked at first, then it has stopped working. 01:27.130 --> 01:28.300 Can you tell me why? 01:28.750 --> 01:31.570 Please post a video now and try to answer. 01:36.710 --> 01:43.610 The al w variable is supposed to keep track of the width of the current line, but here it increases 01:43.610 --> 01:44.210 nonstop. 01:44.390 --> 01:47.480 This causes it to print a lot of unnecessary new lines. 01:48.050 --> 01:53.780 I need to reset it once it prints a new line so that it will be able to keep track of the current line. 01:54.720 --> 02:01.690 Cool, now it works, however, there is still a problem, it cuts some of the lines prematurely. 02:02.220 --> 02:04.260 Let's investigate why that's so. 02:04.950 --> 02:06.830 I'm going to print a marker here for now. 02:08.190 --> 02:11.700 So I can be able to understand where it puts the newlines. 02:14.410 --> 02:18.850 As you can see, it puts a new line here because it hits the maximum line limit. 02:19.770 --> 02:25.920 For example, take a look at this here, the program thinks that the last line contains 40 rooms, but 02:25.920 --> 02:27.210 in reality it doesn't. 02:27.540 --> 02:32.010 It makes a mistake because it counts the total rules in all of these lines. 02:32.220 --> 02:37.770 It does so because it forgets resetting the LW variable, even though the line ends here. 02:38.280 --> 02:41.970 So it keeps counting the rules, even though they are not on the same line. 02:42.600 --> 02:45.180 That's why Primatial ends the line here. 02:45.780 --> 02:48.930 To fix this, I need to check for the new lines like so. 02:50.500 --> 02:55.330 So whenever the next room is a new line, the program will reset the LWR variable. 02:58.400 --> 03:01.230 Oops, now it adds additional new lines. 03:01.700 --> 03:08.900 That happens because here it also prints the new lines and I'm also printing them here, so I'm duplicating 03:08.900 --> 03:09.730 the new lines. 03:10.370 --> 03:14.830 I need to separate the new line detection to ask if Clauss like so. 03:15.440 --> 03:19.100 But I also need to reset the LWR here like so. 03:20.990 --> 03:23.610 Cool, it works, by the way. 03:23.660 --> 03:29.420 Now, I've duplicated the resetting code here, let me convert this to a switch statement instead. 03:29.670 --> 03:31.280 You you'll see why in a second. 03:34.620 --> 03:41.660 OK, here, let's catch two birds with one stone, and I'm going to remove this assignment from here, 03:42.030 --> 03:44.100 let's put a full truth statement here. 03:44.460 --> 03:51.510 So whenever the program puts a new line by itself or when it sees a new line in the text, it will reset 03:51.510 --> 03:52.200 the counter. 03:54.050 --> 04:00.320 As you can see, it still works as before, cool, but there is another problem now see it? 04:01.520 --> 04:05.930 This time, it cuts the words prematurely before even they finish. 04:06.560 --> 04:07.910 How can I fix this? 04:08.310 --> 04:15.380 Well, I think that I need to didak when the words and so that I can only speak the line when a word. 04:15.380 --> 04:19.940 And to do that, I can add another condition here like so. 04:21.000 --> 04:24.150 Let me show you the documentation of Espace function. 04:24.810 --> 04:27.990 It detects whether a room is a space room or not. 04:28.380 --> 04:36.060 The function assumes that these are the space rooms, backslash t, a room backslash and a new line 04:36.060 --> 04:39.730 room backslash, wi a vertical tab room and so on. 04:40.260 --> 04:43.460 Not that the NEWLON is also a space character. 04:43.800 --> 04:47.030 There's going to be a problem for our program anyway. 04:47.100 --> 04:47.640 That's right. 04:49.240 --> 04:55.130 Cool, now it doesn't cut the words before they finish, however, it puts duplicated new lines again. 04:55.660 --> 04:59.340 Let's fix it by adding another condition here like so. 04:59.890 --> 05:05.330 So whenever the current room is a new line, it won't print another duplicated new line here. 05:05.920 --> 05:09.530 Notice that I put the condition before the Espace function. 05:09.940 --> 05:10.810 Do you know why? 05:11.530 --> 05:17.270 It's because when the room is a new line, it is unnecessary to check for the space rules. 05:17.830 --> 05:23.530 So in that case, this arrangement keeps calling the space function entirely. 05:24.170 --> 05:29.720 Remember, calling a function can have a small or a big cost depending on the function. 05:30.250 --> 05:32.590 So it is better to stay local. 05:35.150 --> 05:36.920 Cool, now it works perfectly. 05:38.170 --> 05:42.230 By the way, the Unicode package has a lot of other helper functions as well. 05:43.910 --> 05:50.300 For example, to upper function can change a single multiplied room to uppercase or is punkt function 05:50.300 --> 05:52.550 can detect that the room is a punctuation. 05:53.450 --> 05:57.890 I recommend you to open a new code file and try some of these functions by yourself. 05:58.520 --> 05:59.200 All right. 05:59.210 --> 06:00.030 That's all for now. 06:00.140 --> 06:01.490 See in the next lecture.