Characters - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Characters

Description:

ASCII representation of printable and control characters. 256 codes. char ... Strings can contain the usual printable characters but a string is terminated by ... – PowerPoint PPT presentation

Number of Views:74
Avg rating:3.0/5.0
Slides: 25
Provided by: davidg84
Category:

less

Transcript and Presenter's Notes

Title: Characters


1
Characters
Lect11
  • We have seen the limited use of characters in
    printf to communicate with the user.
  • We will now explore the facilities of C to handle
    characters.
  • C has the data type char which uses one byte.
  • char codes 256 different characters using the
    American Standard Code for Information
    Interchange (ASCII).

2
Character
  • char
  • ASCII representation of printable and control
    characters.
  • 256 codes.
  • char keystroke

3
Printing HELLO
  • include ltstdio.hgt
  • int main()
  • char H72, E69, L76, O79
  • printf("ccccc", H, E, L, L, O)
  • system("PAUSE") return 0
  • Here the letters are obscure and we have to look
    them up in the ASCII table.
  • A to Z 65 to 90, a to z 97 to 122, 0 to 9 48
    to 57

4
Character Assignment
  • The compiler can save us this trouble
  • include ltstdio.hgt
  • int main()
  • char H'H', E'E', L'L', O'O'
  • printf("ccccc",H, E, L, L, O)
  • system("PAUSE") return 0
  • A single character can be assigned using the
    single quotes

5
White Space
  • White space characters are slightly more
    difficult
  • c' ' //space
  • c'\t' //tab
  • c'\n' //newline - start of next line
  • c'\r' //return - start of this line
  • c'\b' //backspace

6
Non-Printing Characters
  • There are a number of non-printing characters
    that are included in the ASCII set, examples
  • c'\a' //alert - beep
  • c'\0' //null - assign zero to c
  • ASCII started as a 128 character set but has been
    expanded to 256 with graphics characters.

7
Strings
  • Outputing messages one character at a time is
    onerous. C allows us to write several characters
    together in a string
  • printf("HELLO")
  • Strings are enclosed in double quotes
  • include ltstdio.hgt
  • int main()
  • char hello"HELLO"
  • printf("s", hello) //note s
  • system("PAUSE") return 0

8
How is a String Handled?
  • char hello"HELLO"
  • The compiler works out how many elements there
    are in hello.
  • How does the printf function know when to stop
    taking data from memory and sending it to the
    screen?
  • We could save the number of elements in the array
    somewhere OR make the last element in the string
    a null.

9
Printing Strings
  • Compiler counts 5 elements 1 for the null and
    sets the last element to null hello5'\0'
  • include ltstdio.hgt
  • int main()
  • int i0 char hello"HELLO"
  • while(helloi!'\0')
  • printf("c", helloi)
  • i
  • system("PAUSE") return 0

10
Character Input
  • We should realize that all input from the
    keyboard is character by character
  • When we input an integer, say 123, we use three
    keystrokes 1 plus 2 plus 3.
  • scanf reads these keystrokes and builds them into
    a number.
  • To tell scanf that we are finished, we press the
    ENTER key.
  • The keyboard presents its data on the input
    stream stdin.

11
Reading Several Numbers
  • To read several numbers from the keyboard, we can
    use one scanf call. The numbers are separated by
    one or more spaces or ENTER
  • scanf("d d",i, j)
  • Valid input
  • 10 SPACE 20 ENTER i10, j20
  • 10 ENTER i10
  • 20 ENTER j20

12
scanf("c", c)
  • scanf("c",c) can be used to read characters
    but as you may have discovered in the laboratory
    there are problems.
  • Each scanf requires you to press ENTER.
  • The program will not continue past the scanf
    unless you have pressed ENTER and all the
    variables have been satisfied.

13
Consider
  • include ltstdio.hgt
  • int main()
  • char c
  • scanf("c",c) printf("d\n",c)
  • scanf("c",c) printf("d\n",c)
  • system("PAUSE") return 0
  • With the input we get
  • a ENTER 97 ASCII code for 'a'
  • 10 ASCII code for '\n'

14
Caution
  • When scanf builds any number from the keyboard
    characters, it ignores the SPACE and ENTER
    characters except to use them to define the end
    of a number, but it leaves '\n' in the input
    stream.
  • When scanf reads a character, it produces the
    next character from the stream even if the next
    character is the ENTER character used to define
    the end of the last user input.
  • In the example, the second scanf gives the user
    the ENTER character '\n'.

15
Reading Over ENTER
  • include ltstdio.hgt
  • int main()
  • char c
  • do
  • scanf("c", c)
  • while(c'\n')
  • printf("d\n", c)
  • system("PAUSE") return 0

16
fflush
  • include ltstdio.hgt
  • int main()
  • char c
  • scanf("c",c)
  • fflush(stdin) //remove any char left in
  • // the stream by scanf
  • printf("d\n", c)
  • scanf("c", c)
  • printf("d\n", c)
  • system("PAUSE") return 0

17
getchar()
  • scanf has its limitations in operation and there
    is a second routine getchar which offers some
    advantages.
  • Prototype int getchar(void)
  • getchar returns an integer. Assigning an integer
    to a character is possible but the integer allows
    for a negative number to be passed back for
    control
  • End of file EOF -1

18
Reading Characters Until ENTER
  • include ltstdio.hgt
  • int main()
  • char c
  • while((cgetchar())!'\n')
  • printf("d\n", c)
  • system("PAUSE") return 0
  • getchar has the same problem as scanf with '\n'.

19
Reading Characters Until EOF
  • while((cgetchar())!EOF)
  • printf("d\n",c)
  • On the PC we can get the EOF by pressing CTRL Z
  • Consider the implications to our area of circle
    program.

20
Assembling an Integer
  • include ltstdio.hgt
  • int main()
  • char c
  • int i0
  • while((cgetchar())!EOF c!'\n' c!' ')
  • ii10c-'0'
  • printf("d\n",i)
  • system("PAUSE") return 0

21
Reading a String
  • include ltstdio.hgt
  • int main()
  • char c20
  • int i0
  • scanf("s", c)
  • printf("s\n", c)
  • system("PAUSE") return 0

22
String Contents
  • Strings can contain the usual printable
    characters but a string is terminated by a space
    or ENTER on input through scanf. Hence strings
    read with scanf contain no spaces.
  • gets will read the spaces
  • char c20
  • gets(c)
  • printf("s\n", c)

23
Searching a String
  • include ltstdio.hgt
  • include ltstring.hgt
  • int main()
  • char throws"HHTTHHTHHHTTTTH"
  • int i, end, heads0, tails0
  • endstrlen(throws)
  • for(i0 iltend i)
  • if(throwsi'H') heads
  • if(throwsi'T') tails
  • printf("d throws d heads and d tails\n",
    end,
  • heads, tails)
  • 15 throws 8 heads and 7 tails

Lect1101.c
24
Libraries
  • stdio contains the input/output routines viz
  • scanf, getchar
  • string contains string manipulation routines,
    viz
  • strlen, strcpy, strcmp
  • ctype contains character manipulation routines
    viz
  • toupper, isalpha
  • stdlib contains string manipulation routines,
    viz
  • atof, atoi
Write a Comment
User Comments (0)
About PowerShow.com