Welcome in this lecture, I'm going to show you how to solve the mascot challenge.
All right, let's get started.
OK, here are the goals.
Let's start with the first goal, OK, then let's check whether there is a user input or not.
If not so, then let's print the message and return.
OK, now let's check out the second go create a buffer and use it as the output.
OK, I'm going to use the input and its length in the following code.
So let the save the input to a variable first, then I'm going to save its length to the size variable.
OK, now I can create the buffer.
Let me make a less with zero length and with a capacity of the size variable Lexar.
OK, let's check out the next go right inputs to the buffer as it is and print it.
OK, I want to analyze the input, bite by bite in a loop like so this way I can direct links inside
the text.
You will see how it works in a minute.
All right.
Now, let me append a bit of the input to the buffer.
So.
And lastly, I'm going to print the buffer to the console like this, remember, Buff is a bit slow,
so that's why I need to convert it to a street value here.
Good.
Now it just prints the input, not that the program prints this output using our buffer, so why we
have created a buffer and then appended bytes to it?
Why didn't I use a string instead?
For example, I can create a string buffer here.
Then I can add the next bite into it like so here is the first reason why I didn't use a street buffer
here, I need the convertible to a string.
However, this is not a very big issue.
It's because for a small number of whites go optimises this behind the scenes.
The main reason why I didn't use a street buffer is that whenever I add a new string to the string buffer,
Gore creates a new string here.
This because string values are immutable.
I mean, you cannot change that.
You can only create a new string value.
However, with a buffer, I allocate it only once and I can change the elements of the same bytes.
So here, if I had used a string buffer instead, it would OK the new string value as much as the number
of what's inside of the input string.
OK, let me take this back.
OK, now let's check out the next goal.
OK, to detect the link, I need the search for HTP pattern inside the input.
This pattern won't change, so let's create a constant to represent it.
Let's also create another constant for the length of the link pattern.
I'll use it in a second inside the loop.
I'm going to search for the link pattern here.
I'm slicing the input text to seven bytes chunks at a time.
That's because I'm searching for HDP and its length is seven bytes.
As you can see, it moves or the input looking for the link pattern here, there is a link pattern so
I can tell that a link starts from here.
Right.
But before doing that, let's find out why there is this error to understand what's going on.
First, let's bring the length of the text.
Then I'm going to dump the Cilliers expression to the console like this.
OK, let's try it.
As you can see, the input text length is for, however, on the last line here, twenty eight plus
seven equals to thirty five.
So it tries to slice the input beyond its length.
That is the cause of the error.
So it is clear that I should not slice the text beyond is lacked to clue that I can check whether the
remaining part of the text is greater than or equal to the length of the linked pattern.
So let me also remove this one.
OK, let's try it.
Cool, now it works without any errors, but do you really understand how this works here?
I get the remaining part of the text, then I check whether there is more room for a link or not.
Only then I continue slicing the input.
So this ensures that I never slice the input beyond its capacity.
Now I can check for a link like so.
Here I compare whether it equals to the link pattern or not.
Cool, it has returned on the part of the input that contains the link.
So now I know the starting position of the link.
It's between 12 and 19 to position cool limos to remove these ones.
I don't need them anymore.
OK, all right.
Let's take a break now.
No worries.
I'm going to continue this project in the next lecture, but I'm going to have coffee first.
See, in the next lecture by.