We are at the end of this section so we should be doing some practice.
So start with problem one says read phrase from the user and output it again.
So typically what we've been doing so far is just sort of reading one thing from the user and then kind
of using that or maybe a couple things string together say a couple of integers with spaces in reading
those but we haven't really done that with entire phrases so want to show how that works here.
So to start this we will see you need a buffer or an array of characters.
OK.
Is usually called the buffer.
It's just another name for Array.
It's going to be some length and does really matter too much but just enough in words call it buffer
say Max buffer.
It's going to be a character array with 256 characters or Max buffer length characters responses.
The ouch ease enter phrase.
This and how we get sort of a bigger string of characters is through this get CNM get function and we
pass it our buffer which is in an array and the max number of characters.
OK.
And it'll actually be minus one because actually what happens and I get will get the all the characters
and then add a null character at the end.
OK.
So that's why we need to have it be minus one.
I mean to account for the null character that will be added K and all we have to do now is output it.
So your input is
just a buffer.
If you're on this say hello to everyone this and your input was Hello everyone's what worked.
So what it did was it actually read 256 characters or up to the new line character.
Right.
So you have to press enter to finish it.
So just read your 256 characters or up to the new.
I just like how I see and ignore it.
If you remember.
So that's it for that question.
And you'll notice that this one didn't have anything to do with dynamic memory yet.
This one will.
So using Problem 1 Make a function that will take in the input phrase and returns the reverse copy of
the phrase and outputs.
OK so we're going to use a function and it's going to return a character a race a new character a race
a whole new bunch of memory from the heap and return you a character pointer to that the new memory
going to call it reverse.
Copy this and it's going to take a C string
and that's it.
In fact it's going to take this string.
Right.
So remember the difference between a character array and a C string is that C string has the no character
at the end.
And they just said that get well actually insert no character into our array.
OK.
So that's why it's going to be a C string.
So that's why we don't need length here.
If it was just a regular array we'd one length but we don't need that here.
So it's copy that.
So what's go well we actually will need the length.
But when you take the length in a different way.
OK.
So let's say it's actually just use it.
See what it looks like.
Sure.
Stars are busy owning pointer.
OK.
So this is going to reverse copy will actually make a new new array pointer.
So it's going to call it a reverse for AIDS Let's say I call it a reverse copy where it passes the buffer.
We're going to use out.
Let's just say that Free's
is to print out the E-string
this.
And since we own the pointer we should be the one who deletes delete pointer.
Freezing this OK.
So that will be our usage of this reverse copy function but now we have to implement it.
So to make this easier since we have to make error verse actually will need the length and I'm going
to use a different function for this and it's going to be I mean we could implement our own string like
function but we need to actually because there is a function called strollin and it's part of kind of
the C++ standard.
But we need to include something called C string.
So if you include C string you get this function to use this function.
So the sloshy get the length of this
and let's say it's actually make our reverse string
that's new dher of and it should be how long.
Well if we know how long the phrase is than it should be the length of the phrase plus one and it's
plus one for the no character.
And so we always have to have one extra spot for that no character.
And or indeed a j variable that says from J.
Close to zero.
See why in a second.
And are you going to loop in reverse for this one.
So I say at length and ask me minus one.
This will be the end of the phrase right and this is the last character of phrase.
And as long as it's greater than or equal to zero I minus minus.
And so we're going kind of backwards through the array and then we say reverse and we'll see.
J This is why we have J.
Because we're going to go forward in the reverse array.
But we're going to go backwards in the phrase or a.
So this one gets high instead.
And then we're increment J this
straight she.
And then we're quite done with reverse string reverse string.
J is equal to the null character because this is a C string and we know it will be at J because once
this is done once it's on the last character this will be incremented sort of kind of point one past
the last character.
So will be pointing at this the length minus or plus 1.
Essentially index.
So then we know we can just use J here k.
It's pointing at the very end anyway and we just sign that to the null character and we just say turn
over string this.
So if you run this
I say hello to you on
this.
And so your input was.
Hello everyone.
Very formatted very nicely.
This
around that
on gay or it was Hello everyone and the phrase Well the reverse of the phrase is right.
You know all this they kind of read it backwards.
So that gives you the reverse.
So in the next section you're going to use what we learn right here to apply to hang man.
It will be our second game.
So I'll see you in the next section.