CPT: Chars/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to look at how C processes characters 4. Character Processing
CPT: Chars/42 Overview 1.Character Variables 2.Printing a Character 3.Reading a Character 4.Two Short Examples 5.A Skeleton for Character I/O 6.Counting Words
CPT: Chars/43 1. Character Variables #include int main() { char c;/* declare a character var */ char c1 = 'A', c2 = 'b', c3 = '*'; c = '1'; return 0; }
CPT: Chars/44 2. Printing a Character 2.1.Using printf() 2.2.Printing ‘Funny’ Characters 2.3.Using putchar()
CPT: Chars/ Using printf() printf("%c", 'a'); printf("%c%c%c", 'A', 'B', 'C'); l A character can be treated as an integer: Character:'a' 'b' 'A' 'B' '1' '2' '&'... Integer: l printf("%d", 'a'); printf("%c", 97);
CPT: Chars/ Printing ‘Funny’ Characters Name of charWritten in CInteger value alert\a7 backspace\b8 carriage return\r13 tab\t9 newline\n10 single quote\'39 l printf("%c", '\a'); /* the machine will beep */
CPT: Chars/ Using putchar() p.873 #include int main() { putchar('S'); putchar('l'); putchar('o'); putchar('w'); putchar('.'); putchar('\n'); return 0; }
CPT: Chars/48 3. Reading a Character 3.1.Using getchar() 3.2. twice.c 3.3.Using twice.c
CPT: Chars/ Using getchar() p.873 /* Repeatedly input characters and print them twice on the screen */ #include int main() { char c; while(1) { /* always true */ c = getchar(); putchar(c); putchar(c); } return 0; }
CPT: Chars/ twice.c /* 2nd version */ #include int main() { int c; while( (c = getchar()) != EOF ) { putchar(c); putchar(c); } return 0; }
CPT: Chars/411 ((c = getchar()) != EOF) is not the same as: (c = (getchar() != EOF))
CPT: Chars/ Using twice.c % gcc -Wall -o twice twice.c % twice aabbccdd /* I typed abcd on the keyboard, followed by a carriage return and d */
CPT: Chars/413 % twice < input.txt aaccddee/* file contains acde */ % twice out.txt % twice > out.txt Other Forms of Input / Output
CPT: Chars/ Two Short Examples 4.1.Copy a File 4.2.Capitalize and Double
CPT: Chars/ Copy a File (ccopy.c) /* Copy from stdin to stdout a character at a time */ # include int main() { int c; while ((c = getchar()) != EOF) putchar(c); return 0; }
CPT: Chars/416 Compile and run % gcc -Wall -o ccopy ccopy.c % ccopy my_proj.txt
CPT: Chars/ Capitalize and Double (caps.c) l Read characters from stdin to stdout –capitalize lowercase letters –double newlines
CPT: Chars/418 #include int main() { int ch; while ((ch = getchar()) != EOF) { if ('a' <= ch && ch <= 'z') putchar(ch + 'A' - 'a'); else if (ch == '\n') { putchar('\n'); putchar('\n'); } else putchar(ch); } return 0; }
CPT: Chars/419 ASCII mapping from lower to upper 'a' = 97 'A' = 65 'b' = 98'B' = 66 : : 'z' = 122'Z' = 90
CPT: Chars/420 /* More portable version */ #include #include /* p.859/520 */ int main() { int ch; while ((ch = getchar()) != EOF) { if (islower(ch)) putchar(toupper(ch)); else if (ch == '\n') { putchar('\n'); putchar('\n'); } else putchar(ch); } return 0; }
CPT: Chars/421 /* Third version */ #include #include int main() { int ch; while ((ch = getchar()) != EOF) { if (ch == '\n') { putchar('\n'); putchar('\n'); } else putchar(toupper(ch)); } return 0; }
CPT: Chars/ A Skeleton for Character I/O /* some comments */ #include #include /* often required */ int process_char(int ch); int main() { int ch, new_ch; while ((ch = getchar()) != EOF) { new_ch = process_char(ch); putchar(new_ch); } return 0; } continued
CPT: Chars/423 int process_char(int ch) /* manipulate the char as an integer */ { /* do something to ch */ return /* an integer, representing a char */ ; }
CPT: Chars/ Counting Words /* Count the words read from stdin. */ #include #include int found_next_word(void); int main() { int word_cnt = 0; while (found_next_word() == 1) word_cnt++; printf("Num words = %d\n", word_cnt); return 0; } continued
CPT: Chars/425 int found_next_word(void) /* Skip spaces, then read the non-white space characters until white space is reached (or EOF). Return 1 if found non-white space characters, 0 otherwise. */ {.... } continued
CPT: Chars/426 int found_next_word(void) { int c; while (isspace( c = getchar() )) ; /* skip white space */ if (c != EOF) { /* found a word */ while ((c = getchar()) !=EOF && !isspace(c)) ; /* skip everything except EOF and white space */ return 1; } return 0; }