Character Input/Output in C: Professor Jennifer Rexford COS 217
Character Input/Output in C: Professor Jennifer Rexford COS 217
Character Input/Output in C
http://www.cs.princeton.edu/courses/archive/spring08/cos217/
1
Overview of Today’s Lecture
• Goals of the lecture
Important C constructs
– Program flow (if/else, loops, and switch)
– Character input/output (getchar and putchar)
Deterministic finite automata (i.e., state machine)
Expectations for programming assignments
• C programming examples
Echo the input directly to the output
Put all lower-case letters in upper case
Put the first letter of each word in upper case
int main(void) {
int c;
c = getchar();
Why an “int”?
putchar(c);
return 0;
}
Why a return value?
4
Why is the Character an “int”
• Meaning of a data type
Determines the size of a variable
… and how it is interpreted and manipulated
• Difference between char and int
char: character, a single byte (256 different values)
int: integer, machine-dependent (e.g., -32,768 to
32,767)
#include <stdio.h>
int main(void) {
int c, i;
#include <stdio.h>
int main(void) {
int c;
for ( ; ; ) {
c = getchar();
putchar(c);
}
return 0;
} 7
Read and Write Till End-Of-File
• Test for end-of-file (EOF)
EOF is a special global constant, defined in stdio
The break statement jumps out of the current scope before the loop
#include <stdio.h>
int main(void) {
int c; do some stuff
for ( ; ; ) {
c = getchar(); done yet?
if (c == EOF)
break;
do more stuff
putchar(c);
}
return 0;
} after the loop 8
Many Ways to Say the Same Thing
for (c=getchar(); c!=EOF; c=getchar())
putchar(c);
11
ASCII
American Standard Code for Information Interchange
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI
16 DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US
32 SP ! " # $ % & ' ( ) * + , - . /
48 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
64 @ A B C D E F G H I J K L M N O
80 P Q R S T U V W X Y Z [ \ ] ^ _
96 ` a b c d e f g h i j k l m n o
112 p q r s t u v w x y z { | } ~ DEL
14
Avoid Mysterious Numbers
#include <stdio.h>
int main(void) {
int c; Correct, but ugly to
for ( ; ; ) { have all these hard-
c = getchar(); wired constants in the
program.
if (c == EOF) break;
if ((c >= 97) && (c < 123))
c -= 32;
putchar(c);
}
return 0;
} 15
Improvement: Character Literals
#include <stdio.h>
int main(void) {
int c;
for ( ; ; ) {
c = getchar();
if (c == EOF) break;
if ((c >= ’a’) && (c <= ’z’))
c += ’A’ - ’a’;
putchar(c);
}
return 0;
} 16
Improvement: Existing Libraries
Standard C Library Functions ctype(3C)
NAME
ctype, isdigit, isxdigit, islower, isupper, isalpha, isalnum, isspace, iscntrl, ispunct, isprint,
isgraph, isascii - character handling
SYNOPSIS
#include <ctype.h> DESCRIPTION
int isalpha(int c);
These macros classify character-
int isupper(int c); coded integer values. Each is a
int islower(int c); predicate returning non-zero for true, 0
int isdigit(int c); for false...
int isalnum(int c); The toupper() function has as a
int isspace(int c); domain a type int, the value of which is
int ispunct(int c); representable as an unsigned char or
the value of EOF.... If the argument of
int isprint(int c);
toupper() represents a lower-case
int isgraph(int c); letter ... the result is the corresponding
int iscntrl(int c); upper-case letter. All other arguments
int toupper(int c); in the domain are returned unchanged.
int tolower(int c); 17
Using the ctype Library
#include <stdio.h>
#include <ctype.h>
int main(void) {
int c;
for ( ; ; ) {
c = getchar();
if (c == EOF) break; Returns 1 (true) if c is
between ‘a’ and ‘z’
if (islower(c))
c = toupper(c);
putchar(c);
}
return 0;
} 18
Compiling and Running
% ls
get-upper.c
% gcc get-upper.c
% ls
a.out get-upper.c
% a.out
We’ll be on time today!
WE’LL BE ON TIME TODAY!
^D
% 19
Run the Code on Itself
% a.out < get-upper.c
#INCLUDE <STDIO.H>
#INCLUDE <CTYPE.H>
INT MAIN(VOID) {
INT C;
FOR ( ; ; ) {
C = GETCHAR();
IF (C == EOF) BREAK;
IF (ISLOWER(C))
C = TOUPPER(C);
PUTCHAR(C);
}
RETURN 0;
}
20
Output Redirection
% a.out < get-upper.c > test.c
% gcc test.c
test.c:1:2: invalid preprocessing directive #INCLUDE
test.c:2:2: invalid preprocessing directive #INCLUDE
test.c:3: syntax error before "MAIN"
etc...
21
Review of Example #2
• Representing characters
ASCII character set
Character constants (e.g., ‘A’ or ‘a’)
• Manipulating characters
Arithmetic on characters
Functions like islower() and toupper()
22
Example #3: Capitalize First Letter
• Capitalize the first letter of each word
“cos 217 rocks” “Cos 217 Rocks”
23
Deterministic Finite Automaton
Deterministic Finite Automaton (DFA)
not-letter letter
letter
1 2
not-letter
• What now?
States should have names, not just 1,2
Should handle each state in a separate function
Good to check for unexpected variable value
29
Improvement: Names for States
30
Improvement: Names for States
#include <stdio.h>
#include <ctype.h>
enum Statetype {NORMAL,INWORD};
int main(void) {
int c; enum Statetype state = NORMAL;
for ( ; ; ) {
c = getchar();
if (c == EOF) break;
switch (state) {
case NORMAL:
if (isalpha(c)) {
putchar(toupper(c));
state = INWORD;
} else putchar(c);
break;
case INWORD:
if (!isalpha(c)) state = NORMAL;
putchar(c);
break;
}
}
return 0; 31
}
Improvement: Modularity
#include <stdio.h>
#include <ctype.h>
enum Statetype {NORMAL,INWORD};
enum Statetype handleNormalState(int c) {...}
enum Statetype handleInwordState(int c) {...}
int main(void) {
int c;
enum Statetype state = NORMAL;
for ( ; ; ) {
c = getchar();
if (c == EOF) break;
switch (state) {
case NORMAL:
state = handleNormalState(c);
break;
case INWORD:
state = handleInwordState(c);
break;
}
}
return 0;
}
32
Improvement: Modularity
enum Statetype handleNormalState(int c) {
enum Statetype state;
if (isalpha(c)) {
putchar(toupper(c));
state = INWORD;
}
else {
putchar(c);
state = NORMAL;
}
return state;
}
33
Improvement: Modularity
enum Statetype handleInwordState(int c) {
enum Statetype state;
putchar(c);
if (!isalpha(c))
state = NORMAL;
else
state = INWORD;
return state;
}
34
Improvement: Defensive Programming
• Assertion checks for diagnostics
Check that that an expected assumption holds
Print message to standard error (stderr) when expression is false
E.g., assert(expression);
Makes program easier to read, and to debug
switch (state) {
case NORMAL:
…
break;
case INWORD:
…
break; Should never,
default:
assert(0);
ever get here.
}
35
Putting it Together: An “A” Effort
#include <stdio.h>
#include <ctype.h>
37
Putting it Together: An “A” Effort
int main(void) {
int c;
enum Statetype state = NORMAL;
for ( ; ; ) {
c = getchar();
if (c == EOF) break;
switch (state) {
case NORMAL:
state = handleNormalState(c);
break;
case INWORD:
state = handleInwordState(c);
break;
}
}
return 0;
} 38
Review of Example #3
• Deterministic Finite Automaton
Two or more states
Actions in each state, or during transition
Conditions for transitioning between states
39
Another DFA Example
• Does the string have “nano” in it?
“banano” yes
“nnnnnnnanofff” yes
“banananonano” yes
“bananananashanana” no
‘n’
‘n’
‘a’
40
Yet Another DFA Example
Question #4 from fall 2005 midterm
Identify whether or not a string is a floating-point number
http://www.cs.princeton.edu/courses/archive/fall05/cos217/exams/fall05exam1ans.pdf 41
Conclusions
• Lectures this week
C fundamentals
Character I/O
Deterministic Finite Automata
• Reading this week
King book: chapters 1-3
• Lectures next week
Variables pointers and arrays
Good programming
• Reading for next week
King book: chapters 4-7
K&P book: chapters 4 and 5
42