Welcome back.
In Texas, editors, usually there is a feature called The Word Wrapping in this lecture.
I'm going to show you how to implement the same feature.
Let's get started.
Now I'm going to say the Turkish translation of the opening lines of a sci fi book, The Hitchhiker's
Guide to the Galaxy, into a constant laakso never printed using a full range loop like.
Remember, a full range look jumps over the roofs of the Gibbons' Trink, so each loop step returns
the next room.
OK, let's say I want to wrap my 40 characters per line.
So I'm going to declare a new constant here like so.
And I'm also going to declare a new variable here.
LW is short for line with by the way, you want to program in your please get used to names with fewer
letters like.
So this variable is the heart of this program.
It keeps track of how long the current line is.
I will use it to wrap the lines.
This type An if statement here.
This will deduct whenever the current line gets greater than the maximum allowed line with.
If so, then I'll print a new line.
Doing so will cut the current line, the rest of the rules will be printed to the next line after printing
a room in a line.
It makes the line longer.
So I also need to increase the Al W.O. variable Lexar.
As you can see, it has worked at first, then it has stopped working.
Can you tell me why?
Please post a video now and try to answer.
The al w variable is supposed to keep track of the width of the current line, but here it increases
nonstop.
This causes it to print a lot of unnecessary new lines.
I need to reset it once it prints a new line so that it will be able to keep track of the current line.
Cool, now it works, however, there is still a problem, it cuts some of the lines prematurely.
Let's investigate why that's so.
I'm going to print a marker here for now.
So I can be able to understand where it puts the newlines.
As you can see, it puts a new line here because it hits the maximum line limit.
For example, take a look at this here, the program thinks that the last line contains 40 rooms, but
in reality it doesn't.
It makes a mistake because it counts the total rules in all of these lines.
It does so because it forgets resetting the LW variable, even though the line ends here.
So it keeps counting the rules, even though they are not on the same line.
That's why Primatial ends the line here.
To fix this, I need to check for the new lines like so.
So whenever the next room is a new line, the program will reset the LWR variable.
Oops, now it adds additional new lines.
That happens because here it also prints the new lines and I'm also printing them here, so I'm duplicating
the new lines.
I need to separate the new line detection to ask if Clauss like so.
But I also need to reset the LWR here like so.
Cool, it works, by the way.
Now, I've duplicated the resetting code here, let me convert this to a switch statement instead.
You you'll see why in a second.
OK, here, let's catch two birds with one stone, and I'm going to remove this assignment from here,
let's put a full truth statement here.
So whenever the program puts a new line by itself or when it sees a new line in the text, it will reset
the counter.
As you can see, it still works as before, cool, but there is another problem now see it?
This time, it cuts the words prematurely before even they finish.
How can I fix this?
Well, I think that I need to didak when the words and so that I can only speak the line when a word.
And to do that, I can add another condition here like so.
Let me show you the documentation of Espace function.
It detects whether a room is a space room or not.
The function assumes that these are the space rooms, backslash t, a room backslash and a new line
room backslash, wi a vertical tab room and so on.
Not that the NEWLON is also a space character.
There's going to be a problem for our program anyway.
That's right.
Cool, now it doesn't cut the words before they finish, however, it puts duplicated new lines again.
Let's fix it by adding another condition here like so.
So whenever the current room is a new line, it won't print another duplicated new line here.
Notice that I put the condition before the Espace function.
Do you know why?
It's because when the room is a new line, it is unnecessary to check for the space rules.
So in that case, this arrangement keeps calling the space function entirely.
Remember, calling a function can have a small or a big cost depending on the function.
So it is better to stay local.
Cool, now it works perfectly.
By the way, the Unicode package has a lot of other helper functions as well.
For example, to upper function can change a single multiplied room to uppercase or is punkt function
can detect that the room is a punctuation.
I recommend you to open a new code file and try some of these functions by yourself.
All right.
That's all for now.
See in the next lecture.