00:01
We want to write a c program with the following characteristics.
00:09
Given a line of text as input, we want to output the number of characters excluding spaces, periods or commons.
00:19
You may assume that the input string will not exceed 15 characters.
00:25
So in this case we will write a main program only, main .c here.
00:32
And let's examine this program to see how the solution is implemented so let's see first we include the standard input output library stdiio .h and the standard library std lib .h those are the headers where the functions correspond to those those libraries are defined.
01:07
After that we have the main program, in this case with a return value to indicate that everything is correct.
01:16
At the end of the program we see return zero as the normality of the end of the execution.
01:24
So we're going to use an unsigned integer variable, constant variable, that is, it means it's a variable, but with the attribute constant constant, it won't, it cannot change value throughout the program.
01:39
This is the case of the variable max shot.
01:41
So it's a variable with the name and the value got to be established when we define the variable or when we declare the variable and the value is 50.
01:51
That's the maximum number of characters we can have or consider on the line we're going to read from screen.
02:01
Then we have an array that will contain the line that is read from the screen and it's also a constant variable.
02:15
In this case, we are going to assign the value after that.
02:20
You can define this array without a constant property, but in this case it means that only means that it's not a dynamic array.
02:33
It's a fixed memory array.
02:37
Okay, we also have a variable for the loop i, which in this case is an unsigned, integer the variable where we are going to store the number of valid characters that is characters that are different from space period and comma and a variable for the actual size of the string str tan okay so we put on screen a message for the user to know he got it got to she got to enter the line of text okay in this case we can add if you want to this message enter a line of text of at most 50 characters if you want and we use here to get as function that is defined in the standard i .o library it's not so reliable this function because the function permits the user to enter a line of more than the number of entries defined for this array line of tests.
03:51
As we see here, that array has 50 entries, because we define these max charts here as 50, and that's the constant variable we are using here.
04:07
So if the user enter more than that, more than 50, the problem is still working, but we don't know exactly what can happen in memory.
04:16
One way to handle that with this type of function get s, get s, is to permit a much larger number of or higher number of characters in the array line of text.
04:33
Instead of 50, we can put, i don't know, 500 and then it doesn't matter if the user enter a long stream because it's unlikely that it is going to enter a a string of 500 characters.
04:50
And when we do that, we use only the 50 first characters as the string to be processed.
04:57
That's the way to work in a better way.
05:02
So like it is done here, if the user enter more than 50 characters, which is very likely to happen, then there can be strange behaviors of the program.
05:14
So in c+ + plus, that is resolved with the get.
05:18
Line function, which we don't have in c, in plain c programs.
05:25
Okay, so we get the string, and then we calculate the actual size of the string.
05:32
So what we do here is to use the operator, question mark, and two points that, as you can see here...