o Echo the input directly to the output o Put all lower-case letters in upper case o Put the first letter of each word in upper case

Similar documents
Reminder. Sign up for ee209 mailing list. Precept. If you haven t received any from ee209 yet Follow the link from our class homepage

C Examples. Goals of this Lecture. Overview of this Lecture

Goals of this Lecture

Princeton University Computer Science 217: Introduction to Programming Systems. Goals of this Lecture. A Taste of C. Agenda.

Princeton University Computer Science 217: Introduction to Programming Systems. A Taste of C

CS341 *** TURN OFF ALL CELLPHONES *** Practice NAME

Appendix A Developing a C Program on the UNIX system

Chapter 8. Characters and Strings

Chapter 2 Bits, Data Types, and Operations

Data Representation and Binary Arithmetic. Lecture 2

Fundamentals of Programming. Lecture 11: C Characters and Strings

Chapter 2 Bits, Data Types, and Operations

Lecture (09) x86 programming 8

CPS 104 Computer Organization and Programming Lecture-2 : Data representations,

Chapter 2 Bits, Data Types, and Operations

Fundamentals of Programming (C)

Fundamentals of Programming

Chapter 2 Bits, Data Types, and Operations

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

ASSIGNMENT 5 TIPS AND TRICKS

Do not start the test until instructed to do so!

SWEN-250 Personal SE. Introduction to C

Numbers and Computers. Debdeep Mukhopadhyay Assistant Professor Dept of Computer Sc and Engg IIT Madras

1.1. INTRODUCTION 1.2. NUMBER SYSTEMS

Simple Data Types in C. Alan L. Cox

LESSON 4. The DATA TYPE char

Introduction to Decision Structures. Boolean & If Statements. Different Types of Decisions. Boolean Logic. Relational Operators

Strings. Steven R. Bagley

Bits and Bytes. Data Representation. A binary digit or bit has a value of either 0 or 1; these are the values we can store in hardware devices.

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Number System (Different Ways To Say How Many) Fall 2016

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Binary Numbers. The Basics. Base 10 Number. What is a Number? = Binary Number Example. Binary Number Example

Week 1 / Lecture 2 8 March 2017 NWEN 241 C Fundamentals. Alvin Valera. School of Engineering and Computer Science Victoria University of Wellington

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

Oberon Data Types. Matteo Corti. December 5, 2001

Fundamental Data Types

Chapter 8 - Characters and Strings

APPENDIX A : KEYWORDS... 2 APPENDIX B : OPERATORS... 3 APPENDIX C : OPERATOR PRECEDENCE... 4 APPENDIX D : ESCAPE SEQUENCES... 5

Chapter 3. Information Representation

Variables and data types

Chapter 2 Bits, Data Types, and Operations

Number Representations

Chapter 2 Number System

Do not start the test until instructed to do so!

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Do not start the test until instructed to do so!

Chapter 8 C Characters and Strings

Tail recursion. Decision. Assignment. Iteration

CS3157: Advanced Programming. Outline

Experiment 3. TITLE Optional: Write here the Title of your program.model SMALL This directive defines the memory model used in the program.

today cs3157-fall2002-sklar-lect05 1

DATA REPRESENTATION. Data Types. Complements. Fixed Point Representations. Floating Point Representations. Other Binary Codes. Error Detection Codes

Number Systems Base r

Chapter 8: Character & String. In this chapter, you ll learn about;

Midterm CSE 131 Fall 2014

EE 109 Unit 3. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

C-programming. Goal To give basic knowledge of the C language. Some previous experiences in programming are assumed. Litterature

EXPERIMENT 8: Introduction to Universal Serial Asynchronous Receive Transmit (USART)

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

Contents. Preface. Introduction. Introduction to C Programming

Lecture 10 Arrays (2) and Strings. UniMAP SEM II - 11/12 DKT121 1

EXPERIMENT 7: Introduction to Universal Serial Asynchronous Receive Transmit (USART)

EE 109 Unit 2. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

Connecting UniOP to Datalogic Barcode Readers

C: How to Program. Week /May/28

Strings in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Appendix B. Comparison of Bourne Shell and C Shell Constructs. Bourne shell. C shell

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

The Binary Number System

Positional Number System

CSE-1520R Test #1. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

5/17/2009. Digitizing Discrete Information. Ordering Symbols. Analog vs. Digital

Midterm CSE 131 Winter 2014

Chapter 1. Hardware. Introduction to Computers and Programming. Chapter 1.2

CSE-1520R Test #1. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

Midterm CSE 131 Winter 2012

CMSC 313 Lecture 03 Multiple-byte data big-endian vs little-endian sign extension Multiplication and division Floating point formats Character Codes

2a. Codes and number systems (continued) How to get the binary representation of an integer: special case of application of the inverse Horner scheme

4/14/2015. Architecture of the World Wide Web. During this session we will discuss: Structure of the World Wide Web

CSE 30 Fall 2007 Final Exam

Characters Lesson Outline

plc numbers Encoded values; BCD and ASCII Error detection; parity, gray code and checksums

1. Character/String Data, Expressions & Intrinsic Functions. Numeric Representation of Non-numeric Values. (CHARACTER Data Type), Part 1

Unit 3. Analog vs. Digital. Analog vs. Digital ANALOG VS. DIGITAL. Binary Representation

Iteration. Side effects

Midterm CSE 131 Winter 2013

CSE 30 Fall 2006 Final Exam

CSE 30 Winter 2009 Final Exam

Universal Asynchronous Receiver Transmitter Communication

CSE 30 Spring 2007 Final Exam

Lecture 03 Bits, Bytes and Data Types

Midterm CSE 131 Winter 2015

CSE 30 Fall 2012 Final Exam

scanf erroneous input Computer Programming: Skills & Concepts (CP) Characters Last lecture scanf error-checking our input This lecture

Programmable #182 Parallel Interface Cash Drawer Manual

Unit 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character

Transcription:

Overview of Today s Lecture Lecture 2: Character Input/Output in C Prof. David August COS 217 http://www.cs.princeton.edu/courses/archive/fall07/cos217/ Goals of the lecture o Important C constructs Program flow (if/else, loops, and switch) Character input/output (getchar and putchar) o Deterministic finite automata (i.e., state machine) o Expectations for programming assignments C programming examples o Echo the input directly to the output o Put all lower-case letters in upper case o Put the first letter of each word in upper case Glossing over some details related to pointers o which will be covered in the next lecture 1 2 Echo Input Directly to Output Including the Standard Input/Output (stdio) library o Makes names of functions, variables, and macros available o Defining procedure main() o Starting point of the program, a standard boilerplate o int main(void) o int main(int argc, char **argv) o Hand-waving: argc and argv are for input arguments Read a single character o Returns a single character from the text stream standard in (stdin) o Write a single character o Writes a single character to standard out (stdout) Putting it All Together Why an int? Why a return value? o 3 4

Why is the Character an int Read and Write Ten Characters Meaning of a data type o Determines the size of a variable o and how it is interpreted and manipulated Loop to repeat a set of lines (e.g., for loop) o Three arguments: initialization, condition, and re-initialization o E.g., start at 0, test for less than 10, and increment per iteration Difference between char and int o char: character, a single byte o int: integer, machine-dependent (e.g., -32,768 to 32,767) One byte is just not big enough o Need to be able to store any character o plus, special value like End-Of-File (typically -1 ) o We ll see an example with EOF in a few slides int c, i; for (i=0; i<10; i++) { 5 6 Read and Write Forever Read and Write Till End-Of-File Infinite for loop o Simply leave the arguments blank o E.g., for ( ; ; ) Test for end-of-file (EOF) o EOF is a special global constant, defined in stdio o The break statement jumps out of the current scope if (c == EOF) before the loop do some stuff done yet? do more stuff 7 after the loop 8

Many Ways to Say the Same Thing Review of Example #1 for (c=getchar(); c!=eof; c=getchar()) while ((c=getchar())!=eof) for (;;) { if (c == EOF) Very typical idiom in C, but messy side-effects in loop test while (c!=eof) { Character I/O o Including stdio.h o Functions getchar() and putchar() o Representation of a character as an integer o Predefined constant EOF Program control flow o The for loop and while loop o The break statement o The return statement Assignment and comparison o Assignment: = o Increment: i++ o Comparing for equality == o Comparing for inequality!= 9 10 Example #2: Convert Upper Case Problem: write a program to convert a file to all upper-case (leave nonalphabetic characters alone) Program design: repeat read a character if it s lower-case, convert to upper-case write the character until end-of-file 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 Lower case: 97-122 and upper case: 65-90 E.g., a is 97 and A is 65 (i.e., 32 apart) 12

Implementation in C That s a B-minus if (c == EOF) if ((c >= 97) && (c < 123)) c -= 32; 13 Programming well means programs that are o Clean o Readable o Maintainable It s not enough that your program works! o We take this seriously in COS 217. 14 Avoid Mysterious Numbers Correct, but ugly to have all these hardwired constants in the program. if (c == EOF) if ((c >= 97) && (c < 123)) c -= 32; 15 Improvement: Character Literals if (c == EOF) if ((c >= a ) && (c <= z )) c += A - a ; 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 int isalpha(int c); int isupper(int c); int islower(int c); int isdigit(int c); int isalnum(int c); int isspace(int c); int ispunct(int c); int isprint(int c); int isgraph(int c); int iscntrl(int c); int toupper(int c); int tolower(int c); DESCRIPTION These macros classify charactercoded integer values. Each is a predicate returning non-zero for true, 0 for false... The toupper() function has as a domain a type int, the value of which is representable as an unsigned char or the value of EOF... If the argument of toupper() represents a lower-case letter... the result is the corresponding upper-case letter. All other arguments in the domain are returned unchanged. 17 Using the ctype Library if (c == EOF) if (islower(c)) c = toupper(c); Returns 1 (true) if c is between a and z 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... Review of Example #2 Representing characters o ASCII character set o Character constants (e.g., A or a ) Manipulating characters o Arithmetic on characters o Functions like islower() and toupper() Compiling and running C code o Compile to generate a.out o Invoke a.out to run program o Can redirect stdin and/or stdout 21 22 Example #3: Capitalize First Letter Deterministic Finite Automaton (DFA) not-letter letter 1 2 not-letter letter State #1: before the 1 st letter of a word State #2: after the 1 st letter of a word Capitalize on transition from state 1 to 2 Implementation Skeleton int main (void) { if (c == EOF) <process one character> cos 217 rocks Cos 217 Rocks 23 24

Implementation <process one character> = case 1: <state 1 action> case 2: <state 2 action> default: <this should never happen> not-letter letter letter 1 2 not-letter if (isalpha(c)) { putchar(toupper(c)); state = 2; else if (!isalpha(c)) state = 1; 25 Complete Implementation int state=1; if (c == EOF) case 1: if (isalpha(c)) { putchar(toupper(c)); state = 2; else case 2: if (!isalpha(c)) state = 1; 26 Running Code on Itself OK, That s a B+ % gcc upper1.c % a.out < upper1.c #Include <Stdio.H> #Include <Ctype.H> Int Main(Void) { Int C; Int State=1; For ( ; ; ) { C = Getchar(); If (C == EOF) Break; Switch (State) { Case 1: If (Isalpha(C)) { Putchar(Toupper(C)); State = 2; Else Putchar(C); Break; Case 2: If (!Isalpha(C)) State = 1; Putchar(C); Break; Return 0; 27 Works correctly, but o Mysterious integer constants ( magic numbers ) o No modularization o No checking for states besides 1 and 2 What now? o States should have names, not just 1,2 o Should handle each state in a separate function o Good to check for unexpected variable value 28

Improvement: Names for States Improvement: Names for States Define your own named constants o Enumeration of a list of items o enum Statetype {NORMAL,INWORD; o Declare a variable of that type o enum Statetype state; 29 enum Statetype {NORMAL,INWORD; enum Statetype state = NORMAL; if (c == EOF) case NORMAL: if (isalpha(c)) { putchar(toupper(c)); state = INWORD; else case INWORD: if (!isalpha(c)) state = NORMAL; 30 Improvement: Modularity Improvement: Modularity enum Statetype {NORMAL,INWORD; enum Statetype handlenormalstate(int c) {... enum Statetype handleinwordstate(int c) {... enum Statetype state = NORMAL; if (c == EOF) case NORMAL: state = handlenormalstate(c); case INWORD: state = handleinwordstate(c); 31 enum Statetype handlenormalstate(int c) { enum Statetype state; if (isalpha(c)) { putchar(toupper(c)); state = INWORD; else { state = NORMAL; return state; 32

Improvement: Modularity enum Statetype handleinwordstate(int c) { enum Statetype state; if (!isalpha(c)) state = NORMAL; else state = INWORD; return state; Improvement: Defensive Programming Assertion checks for diagnostics o Check that that an expected assumption holds o Print message to standard error (stderr) when expression is false o E.g., assert(expression); o Makes program easier to read, and to debug case NORMAL: case INWORD: default: assert(0); Should never, ever get here. 33 34 Putting it Together: An A Effort Putting it Together: An A Effort enum Statetype handleinwordstate(int c) { enum Statetype {NORMAL,INWORD; enum Statetype state; enum Statetype handlenormalstate(int c) { enum Statetype state; if (isalpha(c)) { putchar(toupper(c)); state = INWORD; else { state = NORMAL; return state; 35 if (!isalpha(c)) state = NORMAL; else state = INWORD; return state; 36

Putting it Together: An A Effort Review of Example #3 enum Statetype state = NORMAL; if (c == EOF) case NORMAL: state = handlenormalstate(c); case INWORD: state = handleinwordstate(c); 37 Deterministic Finite Automaton o Two or more states o Actions in each state, or during transition o Conditions for transitioning between states Expectations for COS 217 assignments o Modularity (breaking into distinct functions) o Readability (meaningful names for variables and values) o Diagnostics (assertion checks to catch mistakes) o See K&P book for style guidelines specification 38