Fundamentals of Programming Session 23 Instructor: Reza - - PowerPoint PPT Presentation

fundamentals of programming
SMART_READER_LITE
LIVE PREVIEW

Fundamentals of Programming Session 23 Instructor: Reza - - PowerPoint PPT Presentation

Fundamentals of Programming Session 23 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2014 These slides have been created using Deitels slides Sharif University of Technology Outlines String-Conversion Functions


slide-1
SLIDE 1

Fall 2014

Instructor: Reza Entezari-Maleki

Email: entezari@ce.sharif.edu

Sharif University of Technology

1

Fundamentals of Programming

Session 23

These slides have been created using Deitel’s slides

slide-2
SLIDE 2

Outlines

 String-Conversion Functions  Standard Input/Output Library Functions  String-Manipulation Functions of the String-

Handling Library

 Comparison Functions of the String-

Handling Library

 Structures

2

slide-3
SLIDE 3

 This section presents the string-conversion functions

from the general utilities library (<stdlib.h>).

 These functions convert strings of digits to integer and

floating-point values.

 Figure 8.5 summarizes the string-conversion functions.  Note the use of const to declare variable nPtr in the

function headers (read from right to left as “nPtr is a pointer to a character constant”); const specifies that the argument value will not be modified.

3

String-Conversion Functions

slide-4
SLIDE 4

4

String-Conversion Functions …

slide-5
SLIDE 5

5

String-Conversion Functions …

slide-6
SLIDE 6

6

String-Conversion Functions …

slide-7
SLIDE 7

7

String-Conversion Functions …

slide-8
SLIDE 8

 This

section presents several functions from the standard input/output library (<stdio.h>) specifically for manipulating character and string data.

 Figure

8.12 summarizes the character and string input/output functions of the standard input/output library.

8

Standard Input/Output Library Functions

slide-9
SLIDE 9

9

Standard Input/Output Library Functions …

slide-10
SLIDE 10

10

Standard Input/Output Library Functions …

slide-11
SLIDE 11

11

Standard Input/Output Library Functions …

slide-12
SLIDE 12

12

Standard Input/Output Library Functions …

slide-13
SLIDE 13

 The string-handling library (<string.h>) provides

many useful functions for manipulating string data (copying strings and concatenating strings), comparing strings, searching strings for characters and other strings, tokenizing strings (separating strings into logical pieces) and determining the length of strings.

 This section presents the string-manipulation functions

  • f the string-handling library.

 The functions are summarized in Fig. 8.17.  Every function—except for strncpy—appends the

null character to its result.

13

String-Manipulation Functions of the String- Handling Library

slide-14
SLIDE 14

14

String-Manipulation Functions of the String- Handling Library …

slide-15
SLIDE 15

15

String-Manipulation Functions of the String- Handling Library …

slide-16
SLIDE 16

16

String-Manipulation Functions of the String- Handling Library …

slide-17
SLIDE 17

 This section presents the string-handling library’s

string-comparison functions, strcmp and strncmp.

 Fig. 8.20 contains their prototypes and a brief

description of each function.

17

Comparison Functions of the String-Handling Library

slide-18
SLIDE 18

18

Comparison Functions of the String-Handling Library …

slide-19
SLIDE 19

19

Comparison Functions of the String-Handling Library …

slide-20
SLIDE 20

20

Comparison Functions of the String-Handling Library …

slide-21
SLIDE 21

 This section presents the functions of the string-

handling library used to search strings for characters and other strings.

 The functions are summarized in Fig. 8.22.  The

functions strcspn and strspn return size_t.

21

Search Functions of the String-Handling Library

slide-22
SLIDE 22

22

Search Functions of the String-Handling Library …

slide-23
SLIDE 23

23

Search Functions of the String-Handling Library …

slide-24
SLIDE 24

24

Search Functions of the String-Handling Library …

slide-25
SLIDE 25

25

Search Functions of the String-Handling Library …

slide-26
SLIDE 26

 Function strtok (Fig. 8.29) is used to break a

string into a series of tokens.

 A token is a sequence of characters separated by

delimiters (usually spaces or punctuation marks, but a delimiter can be any character).

 For example, in a line of text, each word can be

considered a token, and the spaces and punctuation separating the words can be considered delimiters.

26

Search Functions of the String-Handling Library …

slide-27
SLIDE 27

27

Search Functions of the String-Handling Library …

slide-28
SLIDE 28

28

Search Functions of the String-Handling Library …

slide-29
SLIDE 29

 The two remaining functions of the string-handling

library are strerror and strlen.

 Figure

8.36 summarizes the strerror and strlen functions.

29

Search Functions of the String-Handling Library …

slide-30
SLIDE 30

30

Search Functions of the String-Handling Library …

slide-31
SLIDE 31

31

Search Functions of the String-Handling Library …

slide-32
SLIDE 32

32

Search Functions of the String-Handling Library …

slide-33
SLIDE 33

 Structures—sometimes referred to as aggregates—

are collections of related variables under one name.

 Structures may contain variables of many different

data types—in contrast to arrays that contain only elements of the same data type.

 Consider the following structure definition:

 struct

struct card { card { char char *face; *face; char char *suit; *suit; }; };

33

Structure Definitions

slide-34
SLIDE 34

 The definition of struct card contains members face

and suit of type char *.

 Structure members can be variables of the primitive data

types (e.g., int, float, etc.), or aggregates, such as arrays and other structures.

 As another example consider the following structure:

 struct

struct employee { employee { char char firstName firstName[ [ 20 20 ]; ]; char char lastName lastName[ [ 20 20 ]; ]; int int age; age; char char gender; gender; double double hourlySalary hourlySalary; }; };

34

Structure Definitions …

slide-35
SLIDE 35

 A structure cannot contain an instance of itself.  For example, a variable of type struct employee cannot be

declared in the definition for struct employee.

 A pointer to struct employee, however, may be included.  For example,

 struct

struct employee2 { employee2 { char char firstName firstName[ 20 ]; [ 20 ]; char char lastName lastName[ 20 ]; [ 20 ]; int int age; age; char char gender; gender; double double hourlySalary hourlySalary; ; struct struct employee2 person; employee2 person; /* ERROR */ /* ERROR */ struct struct employee2 * employee2 *ePtr ePtr; ; /* pointer */ /* pointer */ }; };

 struct employee2 contains an instance of itself (person),

which is an error.

35

Structure Definitions …