240-222 Computer Programming Techniques Semester 1, 1998 - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

240-222 Computer Programming Techniques Semester 1, 1998

Description:

Title: 240-222 Computer Program Techniques Semester 1, 1997 Author: Andrew Davison Last modified by: Andrew Davison Created Date: 5/19/1997 7:39:24 AM – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 27
Provided by: Andrew1650
Category:

less

Transcript and Presenter's Notes

Title: 240-222 Computer Programming Techniques Semester 1, 1998


1
240-222 Computer Programming TechniquesSemester
1, 1998
4. Character Processing
  • Objectives of these slides
  • to look at how C processes characters

2
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
1. Character Variables
  • include ltstdio.hgtint main() char c /
    declare a character var / char c1 'A', c2
    'b', c3 '' c '1' return 0

4
2. Printing a Character
  • 2.1. Using printf()
  • 2.2. Printing Funny Characters
  • 2.3. Using putchar()

5
2.1. Using printf()
  • printf("c", 'a')printf("ccc", 'A', 'B',
    'C')
  • A character can be treated as an integer
  • Character 'a' 'b' 'A' 'B' '1' '2'
    '' . . .Integer 97 98 65 66 49 50
    38 . . .
  • printf("d", 'a')printf("c", 97)

6
2.2. Printing Funny Characters
  • Name of char Written in C Integer
    valuealert \a 7backspace \b 8carriage
    return \r 13tab \t 9newline \n 10si
    ngle quote \' 39
  • printf("c", '\a')/ the machine will beep /

7
2.3. Using putchar() p.873
  • include ltstdio.hgtint main()
    putchar('S') putchar('l') putchar('o')
    putchar('w') putchar('.') putchar('\n')
    return 0

8
3. Reading a Character
  • 3.1. Using getchar()
  • 3.2. twice.c
  • 3.3. Using twice.c

9
3.1. Using getchar() p.873
  • / Repeatedly input characters and print
    them twice on the screen /include
    ltstdio.hgtint main() char c while(1)
    / always true / c getchar()
    putchar(c) putchar(c) return 0

10
3.2. twice.c
  • / 2nd version /include ltstdio.hgtint
    main() int c while( (c getchar()) ! EOF
    ) putchar(c) putchar(c) return
    0

11
  • ((c getchar()) ! EOF)is not the same
    as (c (getchar() ! EOF))

12
3.3. Using twice.c
  • gcc -Wall -o twice twice.c
    twiceaabbccdd/ I typed abcd on the keyboard,
    followed by a carriage return and ltctrlgtd
    /

13
Other Forms of Input / Output
  • twice lt input.txtaaccddee / file contains
    acde /
  • twice lt input.txt gt out.txt
  • twice lt input.txt gtgt out.txt

14
4. Two Short Examples
  • 4.1. Copy a File
  • 4.2. Capitalize and Double

15
4.1. Copy a File (ccopy.c)
  • / Copy from stdin to stdout a character at
    a time / include ltstdio.hgtint main() int
    c while ((c getchar()) ! EOF)
    putchar(c) return 0

16
Compile and run
  • gcc -Wall -o ccopy ccopy.c
  • ccopy lt friends_proj.txt gt my_proj.txt

17
4.2. Capitalize and Double (caps.c)
  • Read characters from stdin to stdout
  • capitalize lowercase letters
  • double newlines

18
  • include ltstdio.hgtint main() int ch while
    ((ch getchar()) ! EOF) if ('a' lt ch
    ch lt 'z') putchar(ch 'A' - 'a')
    else if (ch '\n') putchar('\n')
    putchar('\n') else putchar(ch)
    return 0

19
ASCII mapping from lower to upper
  • 'a' 97 'A' 65'b' 98 'B' 66
    'z' 122 'Z' 90

20
  • / More portable version /include
    ltstdio.hgtinclude ltctype.hgt / 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
  • / Third version /include ltstdio.hgtinclude
    ltctype.hgtint main() int ch while ((ch
    getchar()) ! EOF) if (ch '\n')
    putchar('\n') putchar('\n')
    else putchar(toupper(ch)) return 0

22
5. A Skeleton for Character I/O
  • / some comments /include ltstdio.hgtinclude
    ltctype.hgt / 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
  • int process_char(int ch)/ manipulate the char
    as an integer / / do something to ch
    / return / an integer, representing a
    char /

24
6. Counting Words
  • / Count the words read from stdin. /include
    ltstdio.hgtinclude ltctype.hgtint
    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
  • 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
  • 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
Write a Comment
User Comments (0)
About PowerShow.com