Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand design concepts for fixed-length and variable- length strings ❏

Similar presentations


Presentation on theme: "Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand design concepts for fixed-length and variable- length strings ❏"— Presentation transcript:

1 Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand design concepts for fixed-length and variable- length strings ❏ To understand the design implementation for C-language delimited strings ❏ To write programs that read, write, and manipulate strings ❏ To write programs that use the string functions ❏ To write programs that use arrays of strings ❏ To write programs that parse a string into separate variables ❏ To understand the software engineering concepts of information hiding and cohesion Chapter 11 Chapter 11 Strings Strings

2 Computer Science: A Structured Programming Approach Using C2 11-1 String Concepts In general, a string is a series of characters treated as a unit. Computer science has long recognized the importance of strings, but it has not adapted a standard for their implementation. We find, therefore, that a string created in Pascal differs from a string created in C. Fixed-Length Strings Variable-Length Strings Topics discussed in this section:

3 Computer Science: A Structured Programming Approach Using C3 FIGURE 11-1 String Taxonomy

4 Computer Science: A Structured Programming Approach Using C4 FIGURE 11-2 String Formats

5 Computer Science: A Structured Programming Approach Using C5 11-2 C Strings A C string is a variable-length array of characters that is delimited by the null character. Storing Strings The String Delimiter String Literals Strings and Characters Declaring Strings Initializing Strings Strings and the Assignment Operator Reading and Writing Strings Topics discussed in this section:

6 Computer Science: A Structured Programming Approach Using C6 C uses variable-length, delimited strings. Note

7 Computer Science: A Structured Programming Approach Using C7 FIGURE 11-3 Storing Strings

8 Computer Science: A Structured Programming Approach Using C8 FIGURE 11-4 Storing Strings and Characters

9 Computer Science: A Structured Programming Approach Using C9 FIGURE 11-5 Differences Between Strings and Character Arrays

10 Computer Science: A Structured Programming Approach Using C10 FIGURE 11-6 Strings in Arrays

11 Computer Science: A Structured Programming Approach Using C11 A string literal is enclosed in double quotes. Note

12 Computer Science: A Structured Programming Approach Using C12 FIGURE 11-7 Character Literals and String Literals

13 Computer Science: A Structured Programming Approach Using C13 FIGURE 11-8 String Literal References

14 Computer Science: A Structured Programming Approach Using C14 FIGURE 11-9 Defining Strings

15 Computer Science: A Structured Programming Approach Using C15 Memory for strings must be allocated before the string can be used. Note

16 Computer Science: A Structured Programming Approach Using C16 FIGURE 11-10 Initializing Strings

17 Computer Science: A Structured Programming Approach Using C17 11-3 String Input/Output Functions C provides two basic ways to read and write strings. First, we can read and write strings with the formatted input/output functions, scanf/fscanf and printf/fprintf. Second, we can use a special set of string-only functions, get string (gets/fgets) and put string ( puts/fputs ). Formatted String Input/Output String Input/Output Topics discussed in this section:

18 Computer Science: A Structured Programming Approach Using C18 The string conversion code(s) skips whitespace. Note

19 Computer Science: A Structured Programming Approach Using C19 PROGRAM 11-1Reading Strings

20 Computer Science: A Structured Programming Approach Using C20 The edit set does not skip whitespace. Note

21 Computer Science: A Structured Programming Approach Using C21 Always use a width in the field specification when reading strings. Note

22 Computer Science: A Structured Programming Approach Using C22 The maximum number of characters to be printed is specified by the precision in the format string of the field specification. Note

23 Computer Science: A Structured Programming Approach Using C23 PROGRAM 11-2Demonstrate String Scan Set

24 Computer Science: A Structured Programming Approach Using C24 PROGRAM 11-2Demonstrate String Scan Set

25 Computer Science: A Structured Programming Approach Using C25 PROGRAM 11-3Delete Leading Whitespace

26 Computer Science: A Structured Programming Approach Using C26 PROGRAM 11-3Delete Leading Whitespace

27 Computer Science: A Structured Programming Approach Using C27 PROGRAM 11-4Read Student Names and Scores

28 Computer Science: A Structured Programming Approach Using C28 PROGRAM 11-4Read Student Names and Scores

29 Computer Science: A Structured Programming Approach Using C29 FIGURE 11-11 gets and fgets Functions

30 Computer Science: A Structured Programming Approach Using C30 PROGRAM 11-5Demonstrate fgets Operation

31 Computer Science: A Structured Programming Approach Using C31 PROGRAM 11-5Demonstrate fgets Operation

32 Computer Science: A Structured Programming Approach Using C32 FIGURE 11-12 puts and fputs Operations

33 Computer Science: A Structured Programming Approach Using C33 PROGRAM 11-6Demonstration of Put String

34 Computer Science: A Structured Programming Approach Using C34 PROGRAM 11-6Demonstration of Put String

35 Computer Science: A Structured Programming Approach Using C35 PROGRAM 11-7Typewriter Program

36 Computer Science: A Structured Programming Approach Using C36 PROGRAM 11-7Typewriter Program

37 Computer Science: A Structured Programming Approach Using C37 PROGRAM 11-8Print Selected Sentences

38 Computer Science: A Structured Programming Approach Using C38 PROGRAM 11-8Print Selected Sentences

39 Computer Science: A Structured Programming Approach Using C39 PROGRAM 11-9Print File Double spaced

40 Computer Science: A Structured Programming Approach Using C40 PROGRAM 11-9Print File Double spaced

41 Computer Science: A Structured Programming Approach Using C41 11-4 Arrays of Strings When we discussed arrays of pointers in Chapter 10, we introduced the concept of a ragged array. Ragged arrays are very common with strings. Consider, for example, the need to store the days of the week in their textual format. We could create a two-dimensional array of seven days by ten characters, but this wastes space.

42 Computer Science: A Structured Programming Approach Using C42 PROGRAM 11-10Print Days of the Week

43 Computer Science: A Structured Programming Approach Using C43 PROGRAM 11-10Print Days of the Week

44 Computer Science: A Structured Programming Approach Using C44 FIGURE 11-13 Pointers to Strings


Download ppt "Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand design concepts for fixed-length and variable- length strings ❏"

Similar presentations


Ads by Google