ENCORE5
PAUSE
UTERMINAL
TERMINAL
NEXT
$V1$
SOMECLEAN
ENCORE1
ENCORE
BOX
SOLUTION
GO
Let us look at this example together.
Step 1 is to establish clear goals.
The primary goal is to: determine the number of times the numeric
characters '0-9' occur in the input line.
The secondary goal is to:
Print the input line and the number
of times numerics occur.
We'll divide the process into 3 steps:
1. Read the input from the terminal.
We'll use the function read_line() to do this.
Assume you typed the above line on the terminal screen.
Only 5 digits 4 now 321!
2. Count the number of digits in the above input line.
This will be done by the function digit_count().
3. Print the line and the number of times digits occur.
(See the screen for typical output)
The line entered was:
Only 5 digits 4 now 321!
Digits 0-9 occur 5 times !
The function read_line() has two arguments:
1. s - the character string buffer
2. max - the maximum number of characters
that may be put in the buffer
The skeleton for the function is printed above:
read_line(s, max)
char s[]; /* same as *s */
int max;
{
:
:
:
}
Inside the function we need a character buffer c
and a character counter i.
Of which type should both of these be declared?
Answer here:
That is correct!
You've got it on the 2nd try.
You've got it finally!
They should be a whole numbers.
Remember, whole numbers are integers.
Please review Lesson 2, Topic 8: Constants
FORGET1
The correct choice is int or short since the counter is
an integer and because all characters have integer values. See above.
int i=0; c;
int i=0; c;
The function that will read a single character
from the standard input is:
Answer here:
That is correct!
You've got it on the 2nd try.
You've got it finally!
Name the function that gets a single character.
The function has no arguments and returns a character!
Please review Topic 7 in Lesson 3: The while Statement
FORGET2
The correct choice is getchar(). This is a C library function
for reading a single character from the standard input.
Finally, we want to set the following as LINE delimiters:
1. A new line character '\n',
2. An EOF character, or
3. When max number of characters have been
read into the line buffer s .
The correct statement(s) for reading in a line would be:
a while ((c=getchar()) != '\n' && c != EOF && i < max)
s[i++] = c;
b for (i=0; (c=getchar()) != '\n' && c != EOF && i < max ; i++ )
s[i] = c;
c neither of the above
d both of the above
Select one:
That is correct!
You've got it on the 2nd try.
Good, you understand the concept!
No, try again.
No, try one more time.
Please review Lesson 2, Topic 10: Formatted Text I/O
FORGET3
The correct choice is d . Both statements a and b are CORRECT.
Adding these two statements to our code would give the following:
read_line(s, max)
char s[]; /* same as *s */
int max;
{
int i=0, c;
while ((c = getchar()) != '\n' && c != EOF &&
i < max)
s[i++] = c;
s[i] = '\0'; /* add NULL to end of string */
}
The function digit_count() also has two arguments (two pointers):
1. The address of the character string buffer, s.
2. The address of the numeric counter, *times.
digit_count (s, times)
char s[];
int *times;
{
:
:
:
:
}
Internally we need a string/array index we call int i
int i;
and we set the digit counter to ZERO!
*times = 0;
Note that if we had said: times = 0;
we would have said that the address of the counter is 0,
which is very dangerous!
Which of the following statements does the actual digit counting?
a for (; s[i++] ;)
if ((s[i] >= '0') && (s[i] <= '9'))
(*times)++;
b for (; s[i++] ;)
if ((s[i] >= '0') && (s[i] <= '9'))
*times++;
c a and b
d none of the above
Select one:
That is correct!
You've got it on the 2nd try.
You've got it finally!
No, *times++, increments the pointer, then it gives the value
which is WRONG!
Note (*times)++ refers definitely to the value which is incremented!
* and ++ have the same priority, however they are evaluated RIGHT to LEFT!
Please review Topic 3: Performing Pointer Operations
FORGET4
The correct choice is a because *times++ increments the
pointer first, while (*times)++ increments the value!
Finally the function digit_count assumes the above form:
digit_count(s, times)
char s[];
int *times;
{
int i=0;
*times = 0; /* initially zero occurrences */
for (; s[i++] ;)
if ((s[i] >= '0') && (s[i] <= '9'))
(*times)++;
}
The main() program becomes straightforward, i.e., it calls
the two functions discussed and prints the result!
#define LINE_LEN 80 /* maximum line length */
main()
{
char line[LINE_LEN + 1]; /* line buffer */
int c, number;
printf("Enter new line of text.\n_>");
read_line(line,LINE_LEN); /* char. string as argument */
digit_count(line, &number);
printf("\nIn the line \n%s\ndigits 0-9 occur %d times\n",
line, number);
}