06 - Lecture 6| Character Processing The Data Type char...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lecture 6| Character Processing The Data Type char char? int? Bu ered Input The Use of getchar() and putchar() An Example | Capitalize The Macros in <ctype.h> An Example Getting User's Response Readings Exercises CSC 1500 { Lecture 6 1 Overview Need of character processing: Sex, Choice, ... How characters are stored and manipulated? Characters? Integers? What skills are needed in character processing? CSC 1500 { Lecture 6 2 The Data Type char One of the fundamental data types. Can be thought of as an integer. For example, 65 48 <======> <======> 'A' '0' Which character this integer value represents? Decimal 00 10 20 30 40 50 60 70 80 90 100 110 120 American Standard Code for Information Interchange, ASCII 0 NUL NL DC4 RS ( 2 < F P Z d n x 1 SOH VT NAK US ) 3 = G Q e o y 2 STX NP SYN SP * 4 > H R f p z n 3 ETX CR ETB ! + 5 ? I S ] g q f 4 EOT SO CAN " , 6 @ J T ^ 5 ENQ SI EM # 7 A K U i s g 6 ACK DLE SUB $ . 8 B L V j t ` h r j 7 BEL DC1 ESC % / 9 C M W a k u DEL 8 BS DC2 FS & 0 : D N X b l v 9 HT DC3 GS ' 1 E O Y c m w Some are printable characters (e.g. 'A', '=', ':'). Some are non-printable characters (e.g. newline, bell, tab). CSC 1500 { Lecture 6 3 The Data Type char (con't) Each character is stored in one byte. One byte ;! 8 bits ;! 28 ;! 256 distinct possible values The integer value corresponding to a character is called its ASCII code. (American Standard Code for Information Interchange). 0 0 0 0 0 0 0 0 0 0 0 0 . . . 0 0 1 1 0 0 0 0 . . . 0 1 1 1 1 1 1 1 127 del 0 0 0 0 0 1 1 0 65 66 A B 0 0 0 0 0 0 0 0 1 0 1 0 0 1 2 nul soh stx CSC 1500 { Lecture 6 4 char? int? Some character constants and their integer ASCII values Lowercase ASCII value Uppercase ASCII value Digit ASCII value Other ASCII value printf printf printf printf ("%c", 'a') ("%d", 'a') ("%c", 97) ("\007") 'a' 97 'A' 65 '0' 48 '&' 38 'b' 98 'B' 66 '1' 49 '*' 42 'c' 99 'C' 67 '2' 50 '+' 43 'z' 112 'Z' 90 '9' 57 a is printed. 97 is printed. a is printed. bell! Name of character alert backslash backspace carriage return double quote formfeed horizontal tab newline null character single quote vertical tab Nonprinting or hard-to-print characters Written in C na nn nb nr n" nf nt nn n0 n' nv Integer value 7 92 8 13 34 12 9 10 0 39 11 Escape character (n) Escape sequence (e.g. nn) CSC 1500 { Lecture 6 5 Buffered Input Input from the keyboard is bu ered. The bu er is called input stream, like a river or reservoir. When you press the keys on the keyboard, the corresponding characters are displayed on the monitor screen. However, the characters are not yet passed to our program until the user presses <Enter>. This is to allow the user to edit/modify the typing using the arrow keys, backspace key, delete key, etc. Q. What is passed to our program after pressing <Enter>? A. The nal sequence of characters including the <Enter>. Q. What happens if the user types more than the need of the current input operation of the program? A. The remaining characters are kept for the subsequent input operations of the program. Q. What are the following slides talking about? A. How to get a single character from the user accurately. Note that the techniques are only applicable to character input. Specifically, getting a single character after reading an integer, a oat, etc. CSC 1500 { Lecture 6 6 The Use of getchar() and putchar() getchar(): Gets a character from the input stream (keyboard). putchar(): Puts a character to the output stream (screen). #include <stdio.h> int main(void) { int c while ((c=getchar()) != EOF) { putchar(c) putchar(c) } return 0 } H HH <--- user types H] and <Enter> <--- program prints H], H] and newline <--- program prints newline Hello! <--- user types Hello!] and <Enter> HHeelllloo!! <--- program prints HHeelllloo!!] and newline <--- program prints newline <---------------------- user presses ^Z (CTRL-Z) and <Enter> indicating End-of-File/Input In <stdio.h>, #define EOF (-1) In the above program, int c CSC 1500 { Lecture 6 7 An Example | Capitalize #include <stdio.h> int main(void) { int c while ((c=getchar()) != EOF) if ('a' <= c && c <= 'z') putchar (c + 'A' - 'a') else if (c == '\n') putchar('\n') else putchar(c) return(0) } /* lower case letter? */ /* offset = + 65 - 97 ==> - 32 */ m M Hello! HELLO! <-------------- ^Z is pressed. CSC 1500 { Lecture 6 8 The Macros in <ctype.h> Takes a single int. Returns a single int (a Boolean truth value). The return value is not necessarily 0 or 1, but zero or nonzero. Macro isalpha(c) isupper(c) islower(c) isdigit(c) isalnum(c) isxdigit(c) isspace(c) ispunct(c) isprint(c) isgraph(c) iscntrl(c) isascii(c) Character macros Nonzero (true) is returned if c is a letter c is an uppercase letter c is a lowercase letter c is a digit c is a letter or digit c is a hexadecimal digit c is a white space character c is a punctuation character c is a printable character c is printable, but not a space c is a control character c is an ASCII code The following utilities return an int corresponding to... Function or macro toupper(c) tolower(c) toascii(c) Character macros and functions E ect transformation of c from lowercase to uppercase transformation of c from uppercase to lowercase transformation of c to a 7-bit ASCII code CSC 1500 { Lecture 6 9 An Example #include <stdio.h> #include <ctype.h> int main(void) { int c while ((c=getchar()) != EOF) { if (isalpha(c)) { /* we only process letters */ printf("Lower: %c ", tolower(c)) printf("Upper: %c\n", toupper(c)) } } return(0) } m Lower: m Upper: M X Lower: x Upper: X 4 <--- ignored = <--- ignored <-------------- ^Z is pressed. CSC 1500 { Lecture 6 10 Getting User's Response #include <stdio.h> int main() { int number int reply printf("Please enter a number: ") scanf("%d", &number) printf("Correct (y/n)? ") reply = getchar() if (reply=='Y' || reply=='y') printf("Correct\n") else printf("Incorrect\n") return 0 } c:\> get.exe Please enter a number: 10 Correct (y/n)? Incorrect c:\> The program will not wait for the user input when the prompt \Correct (y/n)? " is displayed. getchar() will grasp the newline (<Enter>) character in the input bu er (left behind the input number 10). 'nn' i.e. reply CSC 1500 { Lecture 6 11 Getting User's Response #include <stdio.h> #include <ctype.h> int main() { int number int reply do { (con't) /* to use isspace() */ /* use int with getchar() */ printf("Please enter a number: ") scanf("%d", &number) printf("Correct (y/n)? ") while ( isspace( reply=getchar() ) ) /* while-loop with empty body */ } while (reply!='y' && reply!='Y') printf("\nThe number you entered is %d.\n", number) return 0 } Please enter a number: 30 Correct (y/n)? n Please enter a number: 10 Correct (y/n)? n Please enter a Correct (y/n)? Please enter a Correct (y/n)? number: 20 a number: 97 y The number you entered is 97. CSC 1500 { Lecture 6 12 Getting User's Response (con't) We have told you how to use a dummy variable to \eat" the extra newline char: #include <stdio.h> int main() { int number char dummy_newline, reply do { printf("Please enter a number: ") scanf("%d", &number) printf("Correct (y/n)? ") /* use a dummy variable to get rid of a *SINGLE* newline */ scanf("%c", &dummy_newline) scanf("%c", &reply) } while (reply!='y' && reply!='Y') printf("\nThe number you entered is %d.\n", number) return 0 } /* use char with scanf() */ Please enter a Correct (y/n)? Please enter a Correct (y/n)? Please enter a Correct (y/n)? number: 30 n number: 20 a number: 97 y The number you entered is 97. CSC 1500 { Lecture 6 13 Getting User's Response (con't) In fact, there is an even cleaner method: #include <stdio.h> int main() { int number char reply do { printf("Please enter a number: ") scanf("%d", &number) printf("Correct (y/n)? ") scanf(" %c", &reply) } while (reply!='y' && reply!='Y') printf("\nThe number you entered is %d.\n", number) return 0 } /* prepend a space before %c */ /* scanf will skip EMPTY SPACES */ /* use char with scanf() */ Please enter a number: 30 Correct (y/n)? n Please enter a number: 10 Correct (y/n)? <tab> <space> ... <newline> ... n Please enter a Correct (y/n)? Please enter a Correct (y/n)? number: 20 a number: 97 y The number you entered is 97. CSC 1500 { Lecture 6 14 Readings Chapter 5, Sections 5.1 { 5.8 Chapter 6, Section 6.3 Exercises Chapter 5, Exercises 1, 2, 5, 6, 12 2 End of Lecture 6 CSC 1500 { Lecture 6 15 ...
View Full Document

Ask a homework question - tutors are online