WEBVTT

00:00.640 --> 00:05.800
We are at the end of this section so we should be doing some practice.

00:05.820 --> 00:13.940
So start with problem one says read phrase from the user and output it again.

00:14.310 --> 00:23.610
So typically what we've been doing so far is just sort of reading one thing from the user and then kind

00:23.610 --> 00:30.390
of using that or maybe a couple things string together say a couple of integers with spaces in reading

00:30.390 --> 00:38.000
those but we haven't really done that with entire phrases so want to show how that works here.

00:38.400 --> 00:48.810
So to start this we will see you need a buffer or an array of characters.

00:49.030 --> 00:49.760
OK.

00:50.270 --> 00:52.660
Is usually called the buffer.

00:52.770 --> 00:57.050
It's just another name for Array.

00:57.310 --> 01:08.330
It's going to be some length and does really matter too much but just enough in words call it buffer

01:09.470 --> 01:11.630
say Max buffer.

01:13.700 --> 01:24.150
It's going to be a character array with 256 characters or Max buffer length characters responses.

01:24.240 --> 01:29.720
The ouch ease enter phrase.

01:31.680 --> 01:48.470
This and how we get sort of a bigger string of characters is through this get CNM get function and we

01:48.470 --> 02:00.030
pass it our buffer which is in an array and the max number of characters.

02:00.080 --> 02:00.910
OK.

02:01.200 --> 02:12.230
And it'll actually be minus one because actually what happens and I get will get the all the characters

02:12.650 --> 02:16.540
and then add a null character at the end.

02:17.270 --> 02:17.810
OK.

02:17.900 --> 02:23.200
So that's why we need to have it be minus one.

02:23.280 --> 02:34.130
I mean to account for the null character that will be added K and all we have to do now is output it.

02:34.180 --> 02:38.740
So your input is

02:41.490 --> 02:42.990
just a buffer.

02:46.680 --> 02:57.570
If you're on this say hello to everyone this and your input was Hello everyone's what worked.

02:57.720 --> 03:07.290
So what it did was it actually read 256 characters or up to the new line character.

03:07.290 --> 03:07.560
Right.

03:07.570 --> 03:11.580
So you have to press enter to finish it.

03:11.580 --> 03:16.340
So just read your 256 characters or up to the new.

03:16.340 --> 03:19.500
I just like how I see and ignore it.

03:19.530 --> 03:21.790
If you remember.

03:22.620 --> 03:25.130
So that's it for that question.

03:25.440 --> 03:29.140
And you'll notice that this one didn't have anything to do with dynamic memory yet.

03:29.400 --> 03:31.440
This one will.

03:31.440 --> 03:41.580
So using Problem 1 Make a function that will take in the input phrase and returns the reverse copy of

03:41.580 --> 03:43.280
the phrase and outputs.

03:44.110 --> 03:54.980
OK so we're going to use a function and it's going to return a character a race a new character a race

03:55.030 --> 04:03.330
a whole new bunch of memory from the heap and return you a character pointer to that the new memory

04:03.760 --> 04:06.240
going to call it reverse.

04:06.330 --> 04:12.940
Copy this and it's going to take a C string

04:15.700 --> 04:16.700
and that's it.

04:16.720 --> 04:19.000
In fact it's going to take this string.

04:19.000 --> 04:19.490
Right.

04:19.650 --> 04:26.050
So remember the difference between a character array and a C string is that C string has the no character

04:26.050 --> 04:27.020
at the end.

04:27.110 --> 04:34.070
And they just said that get well actually insert no character into our array.

04:34.490 --> 04:35.110
OK.

04:35.470 --> 04:37.930
So that's why it's going to be a C string.

04:38.380 --> 04:40.030
So that's why we don't need length here.

04:40.050 --> 04:45.070
If it was just a regular array we'd one length but we don't need that here.

04:45.640 --> 04:55.570
So it's copy that.

04:55.660 --> 05:01.020
So what's go well we actually will need the length.

05:01.020 --> 05:03.600
But when you take the length in a different way.

05:03.740 --> 05:04.510
OK.

05:04.840 --> 05:10.910
So let's say it's actually just use it.

05:10.930 --> 05:12.390
See what it looks like.

05:12.430 --> 05:13.530
Sure.

05:14.360 --> 05:16.950
Stars are busy owning pointer.

05:17.050 --> 05:17.510
OK.

05:17.550 --> 05:24.840
So this is going to reverse copy will actually make a new new array pointer.

05:25.070 --> 05:38.960
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.

05:41.100 --> 05:45.640
We're going to use out.

05:46.010 --> 05:48.140
Let's just say that Free's

05:50.760 --> 05:57.530
is to print out the E-string

06:01.090 --> 06:03.580
this.

06:03.840 --> 06:12.500
And since we own the pointer we should be the one who deletes delete pointer.

06:12.620 --> 06:15.560
Freezing this OK.

06:15.580 --> 06:21.940
So that will be our usage of this reverse copy function but now we have to implement it.

06:22.120 --> 06:29.920
So to make this easier since we have to make error verse actually will need the length and I'm going

06:29.920 --> 06:39.520
to use a different function for this and it's going to be I mean we could implement our own string like

06:39.520 --> 06:48.520
function but we need to actually because there is a function called strollin and it's part of kind of

06:48.630 --> 06:51.580
the C++ standard.

06:51.970 --> 06:58.790
But we need to include something called C string.

06:58.820 --> 07:04.710
So if you include C string you get this function to use this function.

07:04.870 --> 07:07.870
So the sloshy get the length of this

07:10.480 --> 07:17.670
and let's say it's actually make our reverse string

07:20.120 --> 07:30.280
that's new dher of and it should be how long.

07:30.700 --> 07:40.190
Well if we know how long the phrase is than it should be the length of the phrase plus one and it's

07:40.190 --> 07:45.400
plus one for the no character.

07:45.570 --> 07:51.920
And so we always have to have one extra spot for that no character.

07:53.220 --> 07:57.930
And or indeed a j variable that says from J.

07:57.940 --> 07:59.460
Close to zero.

07:59.460 --> 08:01.500
See why in a second.

08:01.500 --> 08:05.000
And are you going to loop in reverse for this one.

08:05.010 --> 08:09.770
So I say at length and ask me minus one.

08:11.580 --> 08:20.590
This will be the end of the phrase right and this is the last character of phrase.

08:21.230 --> 08:27.090
And as long as it's greater than or equal to zero I minus minus.

08:27.090 --> 08:35.730
And so we're going kind of backwards through the array and then we say reverse and we'll see.

08:35.760 --> 08:38.160
J This is why we have J.

08:38.280 --> 08:43.610
Because we're going to go forward in the reverse array.

08:43.830 --> 08:48.620
But we're going to go backwards in the phrase or a.

08:48.720 --> 08:51.430
So this one gets high instead.

08:51.810 --> 08:55.080
And then we're increment J this

08:57.790 --> 09:02.340
straight she.

09:03.080 --> 09:08.590
And then we're quite done with reverse string reverse string.

09:08.720 --> 09:16.280
J is equal to the null character because this is a C string and we know it will be at J because once

09:16.280 --> 09:24.290
this is done once it's on the last character this will be incremented sort of kind of point one past

09:24.290 --> 09:25.790
the last character.

09:25.790 --> 09:31.940
So will be pointing at this the length minus or plus 1.

09:31.970 --> 09:34.800
Essentially index.

09:34.830 --> 09:39.990
So then we know we can just use J here k.

09:40.150 --> 09:48.030
It's pointing at the very end anyway and we just sign that to the null character and we just say turn

09:49.660 --> 09:53.160
over string this.

09:53.350 --> 09:54.940
So if you run this

09:59.300 --> 10:02.080
I say hello to you on

10:05.140 --> 10:07.550
this.

10:07.710 --> 10:10.260
And so your input was.

10:10.260 --> 10:11.680
Hello everyone.

10:11.720 --> 10:14.530
Very formatted very nicely.

10:15.240 --> 10:15.700
This

10:18.760 --> 10:19.180
around that

10:22.480 --> 10:31.980
on gay or it was Hello everyone and the phrase Well the reverse of the phrase is right.

10:31.990 --> 10:36.410
You know all this they kind of read it backwards.

10:36.880 --> 10:39.860
So that gives you the reverse.

10:39.910 --> 10:46.690
So in the next section you're going to use what we learn right here to apply to hang man.

10:46.980 --> 10:48.680
It will be our second game.

10:48.860 --> 10:50.710
So I'll see you in the next section.
