Download presentation
Presentation is loading. Please wait.
Published bySheena Stanley Modified over 8 years ago
1
240-222 CPT: Chars/41 240-222 Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to look at how C processes characters 4. Character Processing
2
240-222 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
3
240-222 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; }
4
240-222 CPT: Chars/44 2. Printing a Character 2.1.Using printf() 2.2.Printing ‘Funny’ Characters 2.3.Using putchar()
5
240-222 CPT: Chars/45 2.1. 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:97 98 65 66 49 50 38... l printf("%d", 'a'); printf("%c", 97);
6
240-222 CPT: Chars/46 2.2. 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 */
7
240-222 CPT: Chars/47 2.3. Using putchar() p.873 #include int main() { putchar('S'); putchar('l'); putchar('o'); putchar('w'); putchar('.'); putchar('\n'); return 0; }
8
240-222 CPT: Chars/48 3. Reading a Character 3.1.Using getchar() 3.2. twice.c 3.3.Using twice.c
9
240-222 CPT: Chars/49 3.1. 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; }
10
240-222 CPT: Chars/410 3.2. twice.c /* 2nd version */ #include int main() { int c; while( (c = getchar()) != EOF ) { putchar(c); putchar(c); } return 0; }
11
240-222 CPT: Chars/411 ((c = getchar()) != EOF) is not the same as: (c = (getchar() != EOF))
12
240-222 CPT: Chars/412 3.3. Using twice.c % gcc -Wall -o twice twice.c % twice aabbccdd /* I typed abcd on the keyboard, followed by a carriage return and d */
13
240-222 CPT: Chars/413 % twice < input.txt aaccddee/* file contains acde */ % twice out.txt % twice > out.txt Other Forms of Input / Output
14
240-222 CPT: Chars/414 4. Two Short Examples 4.1.Copy a File 4.2.Capitalize and Double
15
240-222 CPT: Chars/415 4.1. 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; }
16
240-222 CPT: Chars/416 Compile and run % gcc -Wall -o ccopy ccopy.c % ccopy my_proj.txt
17
240-222 CPT: Chars/417 4.2. Capitalize and Double (caps.c) l Read characters from stdin to stdout –capitalize lowercase letters –double newlines
18
240-222 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; }
19
240-222 CPT: Chars/419 ASCII mapping from lower to upper 'a' = 97 'A' = 65 'b' = 98'B' = 66 : : 'z' = 122'Z' = 90
20
240-222 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; }
21
240-222 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; }
22
240-222 CPT: Chars/422 5. 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
23
240-222 CPT: Chars/423 int process_char(int ch) /* manipulate the char as an integer */ { /* do something to ch */ return /* an integer, representing a char */ ; }
24
240-222 CPT: Chars/424 6. 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
25
240-222 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
26
240-222 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; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.