Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,150 --> 00:00:03,330
Welcome back to another video in our programming course.
2
00:00:04,110 --> 00:00:10,590
And in this video, we're going to continue talking about strings and in particular, we are going to
3
00:00:10,590 --> 00:00:13,560
learn about some input and output functions.
4
00:00:13,650 --> 00:00:17,570
So at this point, we know how to initialize an array and the string.
5
00:00:17,940 --> 00:00:25,380
And in this video, what we are going to do is to take a look at how we can read some input and storied
6
00:00:25,440 --> 00:00:28,680
as a string, as well as how we can print a string.
7
00:00:29,010 --> 00:00:35,160
Just by using one line of code instead of the loop we've used in the previous exercise.
8
00:00:35,730 --> 00:00:42,270
And we also are going to talk about additional input and output functions that we can use for strings.
9
00:00:42,360 --> 00:00:45,780
So that's what we are going to learn in this video.
10
00:00:46,050 --> 00:00:52,620
And actually, this topic is very important because it will help you to proceed with the challenges
11
00:00:52,860 --> 00:00:55,740
that we are about to do in the next videos.
12
00:00:55,860 --> 00:01:03,210
So simply saying with a raise arrays of integers, for example, we know that we can use a loop to run
13
00:01:03,210 --> 00:01:10,950
over all the elements and use the scanning function to read as input values from the user and store
14
00:01:10,950 --> 00:01:14,160
them inside of these elements one by one.
15
00:01:14,400 --> 00:01:21,360
In this for a loop, meaning that for every element in these grades array, we are going to scan in
16
00:01:21,360 --> 00:01:22,290
separately.
17
00:01:22,300 --> 00:01:28,890
And that's exactly what we can see here in these for loop that we are using a scanner function for each
18
00:01:28,890 --> 00:01:30,940
of the elements of the grades array.
19
00:01:31,560 --> 00:01:37,710
And we can say that it will be also the same if we would use an array of floating points or an array
20
00:01:37,710 --> 00:01:38,220
of charges.
21
00:01:38,370 --> 00:01:38,730
Right.
22
00:01:39,200 --> 00:01:40,670
They they use Iranian.
23
00:01:41,300 --> 00:01:43,380
They're running the loop over and over again.
24
00:01:43,410 --> 00:01:48,550
All the elements are simply would be it would simply be the same.
25
00:01:48,570 --> 00:01:48,840
Right.
26
00:01:48,930 --> 00:01:50,610
But what about strings?
27
00:01:50,700 --> 00:01:52,590
Will there be any difference?
28
00:01:52,620 --> 00:01:58,920
Because we previously said that the whole point of arrays of chars with a backslash zero at the end
29
00:01:58,920 --> 00:02:02,700
of it is that we will be able to treat them as strings.
30
00:02:02,850 --> 00:02:09,090
And it will it should provide us with more functionality and more options to work with it.
31
00:02:09,320 --> 00:02:10,290
Well, with ease.
32
00:02:10,410 --> 00:02:14,820
So let's take a look at how we can use strings as an input.
33
00:02:14,940 --> 00:02:20,740
So relink strings as an input from the user is much easier than working with a raise.
34
00:02:20,940 --> 00:02:25,500
Let's say that we want to read some string from the user and the string.
35
00:02:25,520 --> 00:02:27,360
Will we present our first name?
36
00:02:27,720 --> 00:02:31,290
And we will assume that it will be up to nine characters.
37
00:02:31,470 --> 00:02:35,370
So we will, first of all, create an array of characters of size stand right.
38
00:02:35,400 --> 00:02:35,790
Why?
39
00:02:36,060 --> 00:02:41,640
Easy to say stand because we want to keep some space for the last character.
40
00:02:42,230 --> 00:02:43,780
The backslash zero with the end.
41
00:02:43,930 --> 00:02:49,770
And now the way we can read these string from the console and store it inside the first name is very
42
00:02:49,830 --> 00:02:50,850
straightforward.
43
00:02:50,880 --> 00:02:58,250
We just use the scanner function that we are well familiar with and we specify the percentage as to
44
00:02:58,260 --> 00:03:05,580
is a placeholder to read a string from the console and story inside first name.
45
00:03:05,670 --> 00:03:12,150
So once again, here in the first name we can read a string up to nine characters because the tenth
46
00:03:12,150 --> 00:03:17,760
character is held by default for the null character at the end of it to specify that it's a string.
47
00:03:18,060 --> 00:03:23,700
And we are using a percentage as place holder to specify that we are reading a string.
48
00:03:24,240 --> 00:03:31,740
So basically you can also set some limitations on how many characters you would like to read from.
49
00:03:31,830 --> 00:03:35,370
These are from from the user, from the string.
50
00:03:35,760 --> 00:03:42,420
For example, if you know that the limitation will be just of nine characters, let's say not including
51
00:03:42,420 --> 00:03:43,890
the null at the end.
52
00:03:44,340 --> 00:03:48,270
Then you can modify a little bit your scan F to look like this.
53
00:03:48,360 --> 00:03:48,690
Okay.
54
00:03:48,960 --> 00:03:54,650
So this way, when you specify the percentage nine it's specifies.
55
00:03:55,710 --> 00:04:02,790
It's actually it's sort of a security mechanism that specifies what is the length of the string that
56
00:04:02,790 --> 00:04:09,680
we are going to read from the console and story inside the first name or array of character that we
57
00:04:09,780 --> 00:04:12,180
we've created to hold the string.
58
00:04:12,630 --> 00:04:19,140
So if the user happens to write more than nine characters, then you will just read the first nine of
59
00:04:19,140 --> 00:04:26,760
them and they will simply prevent from different other unexpected things that could be happened if the
60
00:04:26,760 --> 00:04:31,340
user inserts something there is that we do not expect.
61
00:04:31,440 --> 00:04:34,560
So to read some word from the user as an input.
62
00:04:34,620 --> 00:04:41,190
We will use the scanner F and the percentage s place holder and after the comma, you type the name
63
00:04:41,190 --> 00:04:44,310
of your string because it represents its address.
64
00:04:44,340 --> 00:04:47,250
And there we are going to store the input.
65
00:04:47,580 --> 00:04:48,660
So I hope that's clear.
66
00:04:48,660 --> 00:04:56,250
We are reading, for example, here we can see the user inserts are at 12 characters are soaring and
67
00:04:56,250 --> 00:04:59,670
the program reads just the first nine of them in storage.
68
00:04:59,960 --> 00:05:05,580
Side of the first name or array of characters in the first name String.
69
00:05:05,960 --> 00:05:09,110
So we've seen how we can use a string as an input.
70
00:05:09,140 --> 00:05:09,560
Right.
71
00:05:09,600 --> 00:05:13,430
And now let's take a look at how we can use it as an output.
72
00:05:13,550 --> 00:05:19,340
So if you want to print a string, it can be simply done in just one line of code.
73
00:05:19,730 --> 00:05:23,980
You could simply use the percentage as a place holder to print your stream.
74
00:05:23,990 --> 00:05:27,020
So instead of just using some loop.
75
00:05:27,050 --> 00:05:27,310
Right.
76
00:05:27,380 --> 00:05:32,390
Like we've done previously to print every character in an array of characters.
77
00:05:32,830 --> 00:05:35,180
Scenes that we know that first name.
78
00:05:35,330 --> 00:05:37,430
Right now it is a string.
79
00:05:37,670 --> 00:05:43,910
And we said that there are additional functionalities that we will be able to use for strings.
80
00:05:44,060 --> 00:05:47,510
Then we can see that in just one line of code.
81
00:05:47,600 --> 00:05:50,060
We can print the whole string.
82
00:05:50,210 --> 00:05:56,750
So, for example, if we have our first name, let's say it's John and we use these print off-line.
83
00:05:56,750 --> 00:05:58,700
So print F percent, the jazz and.
84
00:05:58,920 --> 00:06:02,090
And then we specify the string to print.
85
00:06:02,120 --> 00:06:06,080
Then John will be printed to our council application.
86
00:06:06,230 --> 00:06:12,680
So simply saying we have no for loops, no iterating over each and every one of the elements.
87
00:06:12,950 --> 00:06:15,740
Simply use a percentage as place holder.
88
00:06:16,010 --> 00:06:17,780
And you are ready to go.
89
00:06:17,960 --> 00:06:20,360
And let's add another example.
90
00:06:20,470 --> 00:06:25,130
Basically, we've used a password previously that looked like this.
91
00:06:25,130 --> 00:06:25,520
Right.
92
00:06:26,210 --> 00:06:34,160
And it doesn't matter if we initialize it, if we initialize the string or if we read it from the user
93
00:06:34,160 --> 00:06:40,370
is an input using some scanner command, for example, like we've seen previously, we can still easily
94
00:06:40,370 --> 00:06:45,120
print this string through the screen just by using these primitive commands.
95
00:06:45,350 --> 00:06:53,300
So print out percentages and the name of the string, the array of characters with the null suffix.
96
00:06:53,360 --> 00:06:54,500
So I hope that's clear.
97
00:06:54,510 --> 00:06:56,780
And you see the difference between printing.
98
00:06:57,070 --> 00:07:01,730
I used a string versus printing an array of characters.
99
00:07:01,880 --> 00:07:05,250
And now let's take another look at this caniff example.
100
00:07:05,990 --> 00:07:12,460
So we've seen the this caniff works as expected when we use it in conjunction with the percentage as
101
00:07:12,710 --> 00:07:17,240
place holder and reading the input into a string.
102
00:07:17,390 --> 00:07:22,910
But the problem is that we checked it out only using one word.
103
00:07:22,970 --> 00:07:23,360
Right.
104
00:07:23,510 --> 00:07:27,650
So what will happen if the string is more than just one word?
105
00:07:28,130 --> 00:07:31,790
For example, if we use a hello world text, right.
106
00:07:31,940 --> 00:07:37,220
It simply consists of two words with some space separating between them.
107
00:07:37,820 --> 00:07:44,600
So we will, first of all, create an array of characters of size 20 and then use a regular scanner
108
00:07:44,630 --> 00:07:48,290
function to read some string from the user.
109
00:07:48,290 --> 00:07:52,400
And in this case, it's going to be a string of hello world.
110
00:07:52,520 --> 00:08:00,140
And once this kind of function is done, we expect that all of these string, including the the space
111
00:08:00,140 --> 00:08:03,260
character that is between the Hello and the world words.
112
00:08:03,760 --> 00:08:04,030
Right.
113
00:08:04,250 --> 00:08:10,580
We expect that these whole string will be stored in SDR since we used a second F function.
114
00:08:10,750 --> 00:08:13,940
And now if we use some print F line, right.
115
00:08:13,940 --> 00:08:21,470
To print all the string that is stored in the SDR variable in the SDR, all let's c array of characters
116
00:08:21,470 --> 00:08:28,010
or a string in this case, we will see that if we do, if we run this print off command, then on the
117
00:08:28,010 --> 00:08:36,140
output side we will get just the hello word, not the hello world, but just the hello world only.
118
00:08:36,140 --> 00:08:36,650
Hello.
119
00:08:36,680 --> 00:08:38,410
Without any world.
120
00:08:38,510 --> 00:08:45,950
And what happened here, and it's very important, is that this kind of function can read the first
121
00:08:45,950 --> 00:08:49,040
characters up to the space character.
122
00:08:49,460 --> 00:08:51,780
It's sort of a delimiter for it.
123
00:08:51,890 --> 00:08:58,160
So it will only take the hello's Thring and storeyed inside of this SDR.
124
00:08:58,310 --> 00:09:06,950
And one of the ways to overcome this sort of limitation is by simply using the Gettis function.
125
00:09:07,040 --> 00:09:14,560
So how it looks like very simple, just use the getters function, specify the SDR, where we should
126
00:09:14,560 --> 00:09:21,560
see where we should store the string that we read from the console as input.
127
00:09:21,710 --> 00:09:22,580
And there you go.
128
00:09:22,760 --> 00:09:29,440
Your string will be stored in this D-R, including including all the space characters.
129
00:09:29,440 --> 00:09:34,370
So they are previously disconnected, did not provide us with such functionality.
130
00:09:34,460 --> 00:09:39,140
So if we try to print out what is stored in these are right.
131
00:09:39,170 --> 00:09:42,590
Using the same print to function, the printer is the same here.
132
00:09:43,370 --> 00:09:48,530
We will see that the Hello world as an input will be seen also as an output.
133
00:09:48,800 --> 00:09:52,700
So in this way, you are reading the whole text.
134
00:09:52,790 --> 00:09:56,510
Not just one word until the space character.
135
00:09:56,600 --> 00:09:59,490
So the main difference between F and get s.
136
00:09:59,930 --> 00:10:07,740
Is that get as simply reads text and stores it, including spaces, and they're supposed to scan if
137
00:10:07,740 --> 00:10:13,950
that reads characters of only the first word, just the first word, we read it.
138
00:10:14,070 --> 00:10:19,950
And if there is some program that you've developed that should read as an input, just one word, then
139
00:10:19,950 --> 00:10:22,220
probably you should consider using scanner.
140
00:10:22,440 --> 00:10:28,170
But if you know that it's valid for the user to provide us Treen with more than just one word.
141
00:10:28,620 --> 00:10:35,270
For example, if you want to read both the first name and the last name from the user and then story
142
00:10:35,330 --> 00:10:38,280
in just one string, that's also OK.
143
00:10:38,320 --> 00:10:40,770
But just use the getters function for that.
144
00:10:40,950 --> 00:10:47,340
Oh, and by the way, there is also another friend for the get us function and it's called the Porter's
145
00:10:47,340 --> 00:10:47,730
Function.
146
00:10:48,000 --> 00:10:52,920
So put s stands for put string basically on your screen.
147
00:10:52,920 --> 00:10:53,220
Right.
148
00:10:53,670 --> 00:10:58,200
It allows you to print out on the screen your desired message.
149
00:10:58,830 --> 00:11:04,350
Similarly, like you done it using a print of printed function.
150
00:11:04,500 --> 00:11:12,250
You can do so just using the progress put s SDR and specify what string will be printed out to the screen.
151
00:11:12,390 --> 00:11:14,880
And both of them are pretty much the same.
152
00:11:14,940 --> 00:11:22,260
There is only two, let's say, differences between them and one of them is that the put his function
153
00:11:22,320 --> 00:11:26,560
automatically appends a new line to the end of the string.
154
00:11:26,880 --> 00:11:33,510
When it's printed out to the console, you will see a new line after this, after after these given
155
00:11:33,510 --> 00:11:34,020
string.
156
00:11:34,230 --> 00:11:36,950
And that's opposed to the print of command, right?
157
00:11:36,960 --> 00:11:38,530
It will not by default.
158
00:11:38,580 --> 00:11:39,390
Add a new line.
159
00:11:39,390 --> 00:11:41,460
You need to specify a backslash.
160
00:11:41,490 --> 00:11:46,440
And whenever you want to, to jump one line ahead.
161
00:11:46,710 --> 00:11:52,560
So if you want to print a string, you use print F percentage ask and then you use a backslash.
162
00:11:52,620 --> 00:11:59,920
And at the end of it, to get one line, but put as does not does not require that it already spans
163
00:12:00,000 --> 00:12:02,980
a new line by default to the end of the string.
164
00:12:03,580 --> 00:12:09,810
And the second difference is that the printed function simply allows you to make some more formatting
165
00:12:09,840 --> 00:12:12,400
using that percentage DB percentage F.
166
00:12:12,480 --> 00:12:21,360
And so any, any, any kind of are constructing different messages using other formats like integers
167
00:12:21,360 --> 00:12:22,310
and floating point.
168
00:12:22,800 --> 00:12:28,980
And that's opposed to the put as which is simply used for putting strings to the screen.
169
00:12:29,160 --> 00:12:37,030
So for example, you can use this line of code to print information about a string and to add some for
170
00:12:37,040 --> 00:12:46,080
Matt specifiers for integers and also add some another message like my name is before the string itself
171
00:12:46,080 --> 00:12:46,710
and so on.
172
00:12:46,710 --> 00:12:53,190
And then I use this place holders to make your message a little bit nicer.
173
00:12:53,520 --> 00:12:54,840
So I hope that's clear.
174
00:12:54,870 --> 00:13:01,020
And you understand the difference between the get and they are SCANA F percent, the jazz and the differences
175
00:13:01,020 --> 00:13:10,080
between put s and print F these are not so let's say not so big differences, but it's very important
176
00:13:10,110 --> 00:13:17,600
that you will know all the differences and then you will not get stuck while creating your new programs.
177
00:13:17,730 --> 00:13:21,670
And I guess that's it for this video, guys.
178
00:13:21,720 --> 00:13:25,710
That's it for their input and output introduction using strings.
179
00:13:25,830 --> 00:13:32,760
And I think that even if it took not too much video time, we still happened to cover up a lot in this
180
00:13:32,760 --> 00:13:33,270
video.
181
00:13:33,390 --> 00:13:37,920
And you now have enough tools to start solving exercises on your own.
182
00:13:38,190 --> 00:13:41,570
So let's start with a simple challenge, shall we?
18351
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.