Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topics discussed in this section:

Similar presentations


Presentation on theme: "Topics discussed in this section:"— Presentation transcript:

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

2 FIGURE 11-3 Storing Strings
Computer Science: A Structured Programming Approach Using C

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

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

5 FIGURE 11-6 Strings in Arrays
Computer Science: A Structured Programming Approach Using C

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

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

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

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

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

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

12 Topics discussed in this section:
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 ). Topics discussed in this section: Formatted String Input/Output String Input/Output Computer Science: A Structured Programming Approach Using C

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

14 PROGRAM 11-1 Reading Strings
Computer Science: A Structured Programming Approach Using C

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

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

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

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

19 Delete Leading Whitespace
PROGRAM 11-3 Delete Leading Whitespace Computer Science: A Structured Programming Approach Using C

20 Delete Leading Whitespace
PROGRAM 11-3 Delete Leading Whitespace Computer Science: A Structured Programming Approach Using C

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

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

23 FIGURE 11-11 gets and fgets Functions
Computer Science: A Structured Programming Approach Using C

24 Demonstrate fgets Operation
PROGRAM 11-5 Demonstrate fgets Operation Computer Science: A Structured Programming Approach Using C

25 Demonstrate fgets Operation
PROGRAM 11-5 Demonstrate fgets Operation Computer Science: A Structured Programming Approach Using C

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

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

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

29 PROGRAM 11-7 Typewriter Program
Computer Science: A Structured Programming Approach Using C

30 PROGRAM 11-7 Typewriter Program
Computer Science: A Structured Programming Approach Using C

31 Print Selected Sentences
PROGRAM 11-8 Print Selected Sentences Computer Science: A Structured Programming Approach Using C

32 Print Selected Sentences
PROGRAM 11-8 Print Selected Sentences Computer Science: A Structured Programming Approach Using C

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

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

35 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. Computer Science: A Structured Programming Approach Using C

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

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

38 FIGURE 11-13 Pointers to Strings
Computer Science: A Structured Programming Approach Using C

39 Topics discussed in this section:
String Manipulation Functions Because a string is not a standard type, we cannot use it directly with most C operators. Fortunately, C provides a set of functions to manipulates strings. Topics discussed in this section: String Length and String Copy String Compare and String Concatenate Character in String Search for a Substring and Search for Character in Set String Span and String Token String to Number Computer Science: A Structured Programming Approach Using C

40 PROGRAM 11-11 Add Left Margin
Computer Science: A Structured Programming Approach Using C

41 PROGRAM 11-11 Add Left Margin
Computer Science: A Structured Programming Approach Using C

42 FIGURE String Copy Computer Science: A Structured Programming Approach Using C

43 FIGURE 11-15 String-number Copy
Computer Science: A Structured Programming Approach Using C

44 Always use strncpy to copy one string to another.
Note Always use strncpy to copy one string to another. Computer Science: A Structured Programming Approach Using C

45 Build Name Array in Heap
PROGRAM 11-12 Build Name Array in Heap Computer Science: A Structured Programming Approach Using C

46 Build Name Array in Heap
PROGRAM 11-12 Build Name Array in Heap Computer Science: A Structured Programming Approach Using C

47 Build Name Array in Heap
PROGRAM 11-12 Build Name Array in Heap Computer Science: A Structured Programming Approach Using C

48 FIGURE 11-16 Structure for Names Array
Computer Science: A Structured Programming Approach Using C

49 FIGURE 11-17 String Compares
Computer Science: A Structured Programming Approach Using C

50 Results for String Compare
Table 11-1 Results for String Compare Computer Science: A Structured Programming Approach Using C

51 FIGURE 11-18 String Concatenation
Computer Science: A Structured Programming Approach Using C

52 FIGURE 11-19 Character in String (strchr)
Computer Science: A Structured Programming Approach Using C

53 FIGURE 11-20 String in String
Computer Science: A Structured Programming Approach Using C

54 FIGURE String Span Computer Science: A Structured Programming Approach Using C

55 FIGURE Streams Computer Science: A Structured Programming Approach Using C

56 Demonstrate String to Long
PROGRAM 11-13 Demonstrate String to Long Computer Science: A Structured Programming Approach Using C

57 Demonstrate String to Long
PROGRAM 11-13 Demonstrate String to Long Computer Science: A Structured Programming Approach Using C

58 Demonstrate String to Long
PROGRAM 11-13 Demonstrate String to Long Computer Science: A Structured Programming Approach Using C

59 String-to-Number Functions
Table 11-2 String-to-Number Functions Computer Science: A Structured Programming Approach Using C

60 FIGURE 11-23 Parsing with String Token
Computer Science: A Structured Programming Approach Using C

61 Parsing a String with String Token
PROGRAM 11-14 Parsing a String with String Token Computer Science: A Structured Programming Approach Using C

62 Parsing a String with String Token
PROGRAM 11-14 Parsing a String with String Token Computer Science: A Structured Programming Approach Using C

63 Compare Packed String Function
PROGRAM 11-15 Compare Packed String Function Computer Science: A Structured Programming Approach Using C

64 Compare Packed String Function
PROGRAM 11-15 Compare Packed String Function Computer Science: A Structured Programming Approach Using C

65 Compare Packed String Function
PROGRAM 11-15 Compare Packed String Function Computer Science: A Structured Programming Approach Using C

66 Compare Packed String Function
PROGRAM 11-15 Compare Packed String Function Computer Science: A Structured Programming Approach Using C

67 Topics discussed in this section:
String/Data Conversion A common set of applications format data by either converting a sequence of characters into corresponding data types or vice versa. Two such applications are parsing and telecommunications. Topics discussed in this section: String to Data Conversion Data to String Conversion Computer Science: A Structured Programming Approach Using C

68 FIGURE 11-24 sscanf Operation
Computer Science: A Structured Programming Approach Using C

69 Note sscanf is a one-to-many function. It splits
one string into many variables. Computer Science: A Structured Programming Approach Using C

70 FIGURE 11-25 sprintf Operation
Computer Science: A Structured Programming Approach Using C

71 Demonstrate Memory Formatting
PROGRAM 11-16 Demonstrate Memory Formatting Computer Science: A Structured Programming Approach Using C

72 Demonstrate Memory Formatting
PROGRAM 11-16 Demonstrate Memory Formatting Computer Science: A Structured Programming Approach Using C

73 Testing for Open and Close Errors
PROGRAM 11-17 Testing for Open and Close Errors Computer Science: A Structured Programming Approach Using C

74 Testing for Open and Close Errors
PROGRAM 11-17 Testing for Open and Close Errors Computer Science: A Structured Programming Approach Using C

75 Testing for Open and Close Errors
PROGRAM 11-17 Testing for Open and Close Errors Computer Science: A Structured Programming Approach Using C


Download ppt "Topics discussed in this section:"

Similar presentations


Ads by Google