processing character data
text stream :
- a sequence of characters divided into lines
- characters + newline
getchar
- reads the next input character from a text stream
- returns its value
c = getchar();
// the variable c contains the next character of input
putchar
- prints a character each time it is called
- interleaved with printf
putchar(c);
// prints the contents of the integer variable c as a character, usually on the screen
1.5.1 File Copying
a program that copies its input to its output one character at a time
read a character
while (character is not end-of-file indicator)
---- output the character just read
---- read a character
#include <stdio.h>
/* copy input to output; 1st version */
int main (void) {
int c;
c = getchar();
while (c != EOF) {
putchar(c);
c = getchar();
}
}
!=
- not equal to
EOF
- end of file
#include <stdio.h>
/* copy input to output; 2nd version */
int main (void) {
int c;
while ((c = getchar()) != EOF)
putchar(c);
}
while
- gets a character
- assigns it to c
- tests whether the character is the end-of-file signal
3.1. if not, the body of the while execute
3.2 print the character
3.3. while repeats - if the end of file is reached
4.1 while terminates
4.2 main terminates
precedence : != > =(assignment)
c = getchar() != EOF
= c = (getchar() != EOF)
// 0 or 1
Exercise 1-6
verify getchar() != EOF
is 0 or 1
#include <stdio.h>
int main (void) {
int c;
while (c = getchar() != EOF)
printf("%d\n", c);
printf("%d - at EOF\n", c);
}
-
getchar
has a character to read and does not return the end of file
'getchar() != EOF' is true
c = 1 - program encounters the end of file
the expression is false
c = 0
loop terminates
Exercise 1-7
write a program to print the value of EOF
#include <stdio.h>
int main (void) {
printf("EOF is %d\n", EOF); // EOF is -1
}
1.5.2 Character Counting
This program counts characters:
#include <stdio.h>
/* count characters in input; 1st edition */
int main (void){
long nc = 0;
while (getchar() != EOF)
++nc;
printf("%ld\n", nc);
return 0;
}
++
increment by one
nc = nc + 1
++
- concise
- efficient
prefix operators ++nc
postfic operators nc++
long
- 32 bits
%ld
int
- 16 bits
- max 32767
double
%f
float
- double precision
%f
#include <stdio.h>
/* count characters in input; 2nd edition */
int main (void){
double nc = 0;
for (nc = 0; getchar() != EOF; ++nc);
printf("%.0f\n", nc);
return 0;
}
%.0f
- print of the decimal point and the fraction part
null statement - the isolated semicolon
while for
- boundary condition (0 length input)
- test at the top of the loop
- proceed to the loop body
1.5.3 Line Counting
the standard library - ensures -
an input stream = a sequence of lines + newline
-> line counting = counting newlines
#include <stdio.h>
/* count lines in input */
int main (void) {
int c, nl = 0;
while ((c == getchar()) != EOF)
if (c == '\n')
++nl;
printf("%d\n", nl);
return 0;
}
while
controls if
controls ++nl
increment
if
- test the parenthesized condition
- if the condition is true, executes the statements
is equal to
Language | Symbol |
---|---|
C | == |
Pascal | = |
4Fortan | .EQ. |
assignment
- C
=
character constant
- 'a character'
- an integer value equal to the numerical value of the character in the machine's character set
Symbol | ASCII Value |
---|---|
'A' | 65 |
'\n' |
10 |
\n
- a single character
- an integer in expressions
- or a string constant
Exercise 1-8
count:
- blanks
- tabs
- newlines
#include <stdio.h>
/* count blanks, tabs, and newlines */
int main (void) {
int c, nb = 0, nt = 0, nl = 0; //number of blanks, tabs, newlines
while ((c == getchar()) != EOF)
if (c == ' ') ++nb;
else if (c == '\t') ++nt;
else if (c == '\n') ++nl;
printf("%d %d %d\n", nb, nt, nl);
return 0;
}
Exercise 1-9
- copy its input to its output
- replace each string of one or more blanks by a single blank
#include <stdio.h>
/* copy input to output; 2nd version */
int main (void) {
int c;
while ((c = getchar()) != EOF)
putchar(c);
}
网友评论