Characters - PowerPoint PPT Presentation

1 / 32
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:102
Avg rating:3.0/5.0
Slides: 33
Provided by: davidg84
Category:

less

Transcript and Presenter's Notes

Title: Characters


1
Characters
  • 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
  • void main()
  • char H72, E69, L76, O79
  • printf("ccccc", H, E, L, L, O)
  • 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
  • void main()
  • char H'H', E'E', L'L', O'O'
  • printf("ccccc",H, E, L, L, O)
  • 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. The
    128 character ASCII set is on page 891 in
    DeitelDeitel.

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
  • void main()
  • char hello"HELLO"
  • printf("s", hello) //note s

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
  • void main()
  • int i0 char hello"HELLO"
  • while(helloi!'\0')
  • printf("c", helloi)
  • i

10
String Pointer
  • We can do the same job with a pointer to the
    string
  • include ltstdio.hgt
  • void main()
  • int i0 char sptr"HELLO"
  • while(sptri!'\0')
  • printf("c",sptri)
  • i

11
printf
  • The first term in printf is defined as a pointer
    to the string which gives the format, consider
  • include ltstdio.hgt
  • void main()
  • char fptr"s", hello"HELLO"
  • printf(fptr, hello)

12
Reading Qualifying Lab1
  • include ltstdio.hgt
  • void main()
  • int Lab1
  • do
  • printf("Input Lab1 marks ")
  • scanf("d", Lab1)
  • while(Lab1lt0 Lab1gt100)

13
reader
  • void reader(char sptr, int ptr)
  • do
  • printf("Input s marks ", sptr)
  • scanf("d", ptr)
  • while(ptrlt0 ptrgt100)

14
main
  • include ltstdio.hgt
  • void reader(char, int)
  • void main()
  • int Lab1, MT
  • char slptr"Lab1", smptr"Midterm"
  • reader(slptr, Lab1)
  • reader(smptr, MT)

15
Calls to reader
380
Lab1
382
MT
a
L
b
1
'\0'
384
i
M
d
t
r
e
m
'\0'
389
reader(slptr, Lab1) reader(384, 380)
reader(smptr, MT) reader(389, 382)
16
Alternate main
  • include ltstdio.hgt
  • void reader(char, int)
  • void main()
  • int Lab1, MT
  • reader("Lab1", Lab1)
  • reader("Midterm", MT)

17
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.

18
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

19
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.

20
Consider
  • include ltstdio.hgt
  • void main()
  • char c
  • scanf("c",c) printf("d\n",c)
  • scanf("c",c) printf("d\n",c)
  • With the input we get
  • a ENTER 97 ASCII code for 'a'
  • 10 ASCII code for '\n'

21
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'.

22
Reading Over ENTER
  • include ltstdio.hgt
  • void main()
  • char c
  • do
  • scanf("c", c)
  • while(c'\n')
  • printf("d\n", c)

23
fflush
  • include ltstdio.hgt
  • void 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)

24
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

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

26
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.

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

28
Reading a String
  • include ltstdio.hgt
  • void main()
  • char c20, sptrc0
  • int i0
  • scanf("s", c)
  • printf("s\n", c)
  • scanf("s", sptr)
  • printf("s\n", sptr)

29
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)

30
Searching a String
  • include ltstdio.hgt
  • include ltstring.hgt
  • void 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

31
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

32
  • stdlib contains string manipulation routines,
    viz
  • atof, atoi
  • conio (a Borland library) contains character
    routines, viz
  • getch
Write a Comment
User Comments (0)
About PowerShow.com