Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Character Processing How characters can be treated as small integers? How characters are stored and manipulated in a machine? How use is made of certain.

Similar presentations


Presentation on theme: "1 Character Processing How characters can be treated as small integers? How characters are stored and manipulated in a machine? How use is made of certain."— Presentation transcript:

1 1 Character Processing How characters can be treated as small integers? How characters are stored and manipulated in a machine? How use is made of certain standard header files?

2 2 Fundamental Data Type stored in one Byte. 2 8 =256 different combos. Letters, Digits, Special Characters. A character constant is written between single quotes. char c = ‘s’; Data Type char

3 3 In C a char has the integer value corresponding to the Binary Coding Scheme in effect (ASCII). See page 174. Letters: ’a’, ‘b’ ……….’z’ 97 98 ……….112 ‘A’, ‘B’ ………’Z’ 65 66 ………. 90 Digits:‘0’, ‘1’ ……….. ‘9’ 48 49 57 Others:‘&’‘+’ ……… 3843

4 4 A “ %c” is used to designate the char format. The content of the Byte can be thought of as either a character or as a small integer. printf (“%c”, ‘a’) ? printf (“%d”, ‘a’) ? printf (“%c”, 98) ?

5 5 Nonprinting and Hard-to-print Characters Escape sequence: (see pg. 176). ‘\n’ - new line, ‘\t’ - tab ‘\\’ - get backslash ‘\”’ - get double quote mark ‘\” - get single quote mark printf(“\“ABC\””); “ABC” printf(“\‘ABC\’”); ‘ABC’ printf(“\n\tThis is a test”);

6 6 getchar( ) reads a character from the keyboard. int getchar(void); R eturns the ORDINAL VALUE of the character read. putchar( ) writes a character to the screen. int putchar(int c); Functional Prototype C is an Integer Valued Expression representing the code of the character to be output. I/O of char

7 7 p.g. 178 How Terminate ____? ctrl+c ctrl+d(unix), ctrl+z(dos) char c; while (1){ c = getchar(); putchar(c);} int c; while ((c = getchar()) != EOF){ putchar(c);}

8 8 stdio.h #define EOF(-1) How interpret EOF? Negative integer stored in 2’s compliment form. 1. Take Pos value (+1) 00000001 2. Reverse Bits 11111110 3. Add 1 +1 11111111 EOF

9 9 Capitalize Lowercase Letters p.g. 183 #include int c; while ((c = getchar()) != EOF){ if (islower(c)) putchar(toupper(c)); }

10 10 ctype.h Header file Contains macros & prototypes of functions that are often used when processing characters: macros - The C preprocessor recognizes lines of the source text that begin with ________? The macros in ctype.h are used to test chars & includes a set of FP’s of functions that are used to convert characters. # # include

11 11 Use of Macros 1) Improve I/O: # define READ(c) c = getchar( ) If (READ(c) == ‘x’) Expand to : If ((c = getchar( )) ==‘x’)

12 12 2) Define some often used operations: # define SWAP(val1, val2, temp) {temp = val1; val1=val2; val2 = temp;} int i = 4, j = 8, temp; SWAP( i, j, temp); printf(“%d %d %d\n”, i, j, temp);

13 13 3)Conditional Compilation Directives: # define IBMPC 1 # if IBMPC # include # else # include # endif IF target machine is IBMPC use IBM specific routines else use machine independent routines Do what if not using IBMPC _________? Change 1 to Zero

14 14 Classifying Functions They examine a character an tell if it belongs to a given class: int is… (int testchar); General Prototype

15 15 iscntrl- The ASCII control chars are all the values below the space(32) and the delete char(127). True if one of these-false otherwise. iscntrlisprint- Is printable, the compliment of iscntrl. True if > 31 and < 127. isspace- Checks for whitespace(blank(32), tab(9), line feed(10), vertical tab(11), form feed(12), carriage return(13)).

16 16 isgraph- All ASCII chars > 32(space) and less than 127(delete) are considered graphic characters. isalnum- The alphabetic characters and the numeric digits are considered the alphanumeric set. ispunct- The graphic complement of the alphanumeric chars. If testchar is > 32 (space) and < 127 (delete) but NOT an alphanumeric, it returns true.

17 17 isalpha- The upper and lower case alphabetic characters. islower- Lower case letter. isupper- Upper case letters. isdigit- The decimal digits. isxdigit- Test for hexidecimal digits, (0..9, a..f, A..F).


Download ppt "1 Character Processing How characters can be treated as small integers? How characters are stored and manipulated in a machine? How use is made of certain."

Similar presentations


Ads by Google