fundamentals of programming
play

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


  1. Fundamentals of Programming Session 23 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2014 These slides have been created using Deitel’s slides Sharif University of Technology

  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

  3. String-Conversion Functions  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

  4. String- Conversion Functions … 4

  5. String- Conversion Functions … 5

  6. String- Conversion Functions … 6

  7. String- Conversion Functions … 7

  8. Standard Input/Output Library Functions  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

  9. Standard Input/Output Library Functions … 9

  10. Standard Input/Output Library Functions … 10

  11. Standard Input/Output Library Functions … 11

  12. Standard Input/Output Library Functions … 12

  13. String-Manipulation Functions of the String- Handling Library  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 of 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

  14. String-Manipulation Functions of the String- Handling Library … 14

  15. String-Manipulation Functions of the String- Handling Library … 15

  16. String-Manipulation Functions of the String- Handling Library … 16

  17. Comparison Functions of the String-Handling Library  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

  18. Comparison Functions of the String-Handling Library … 18

  19. Comparison Functions of the String-Handling Library … 19

  20. Comparison Functions of the String-Handling Library … 20

  21. Search Functions of the String-Handling Library  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. strcspn strspn  The functions and return size_t . 21

  22. Search Functions of the String-Handling Library … 22

  23. Search Functions of the String-Handling Library … 23

  24. Search Functions of the String-Handling Library … 24

  25. Search Functions of the String-Handling Library … 25

  26. Search Functions of the String-Handling Library …  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

  27. Search Functions of the String-Handling Library … 27

  28. Search Functions of the String-Handling Library … 28

  29. Search Functions of the String-Handling Library …  The two remaining functions of the string-handling library are strerror and strlen . strerror  Figure 8.36 summarizes the and strlen functions. 29

  30. Search Functions of the String-Handling Library … 30

  31. Search Functions of the String-Handling Library … 31

  32. Search Functions of the String-Handling Library … 32

  33. Structure Definitions  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 *face; char *face; char char *suit; *suit; }; }; 33

  34. Structure Definitions …  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

  35. Structure Definitions …  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 lastName char lastName[ 20 ]; [ 20 ]; int int age; age; char char gender; gender; double hourlySalary double 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend