Chapter 9 Strings Dr Jiungyao Huang Dept Comm

  • Slides: 72
Download presentation
Chapter 9 Strings Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail

Chapter 9 Strings Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu. edu. tw TA: 鄭筱親 陳昱豪 Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1 -

本章重點 • Null-terminated char array – Consecutive characters, ended with ‘�’ • Array of

本章重點 • Null-terminated char array – Consecutive characters, ended with ‘’ • Array of string: Array of array? array of pointer ? • 指標的加法與減法, 比較: Appendix D • 作站上控制游標, 以及單鍵輸入 (curses library) Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 2

Outline • 9. 1 STRING BASICS • 9. 2 STRING LIBRARY FUNCTIONS: ASSIGNMENT AND

Outline • 9. 1 STRING BASICS • 9. 2 STRING LIBRARY FUNCTIONS: ASSIGNMENT AND SUBSTRINGS • 9. 3 LONGER STRINGS: CONCATENATION AND WHOLELINE INPUT • 9. 4 STRING COMPARISON • 9. 5 ARRAYS OF POINTERS • 9. 6 CHARACTER OPERATIONS • 9. 7 STRING-TO-NUMBER AND NUMBER-TO STRING CONVERSIONS • 9. 8 STRING PROCESSING ILLUSTRATED – CASE STUDY: • TEXT EDITOR • 9. 9 COMMON PROGRAMMING ERRORS Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 3

9. 1 STRING BASICS • Declaring and initializing string variables – A string in

9. 1 STRING BASICS • Declaring and initializing string variables – A string in C is implemented as a char array – Null character • Character ‘’ that marks the end of a string in C – For example • char str[20] = “Initial value”; • A string constant can be associated with a symbolic name using the #define directive [0] [4] I n i t i a l Copyright © 2004 Pearson Addison-Wesley. All rights reserved. [9] [14] [19] v a l u e ? ? ? 4

ARRAYS OF STRINGS • An array of strings can be defined as a twodimensional

ARRAYS OF STRINGS • An array of strings can be defined as a twodimensional array of characters in which each row is one string. • For example – – #define NUM_PEOPLE 30 #define NAME_LEN 25 char names [NUM_PEOPLE][NAME_LEN]; char month[12][10] = { “enero”, “febrero”, “marzo”, “abril”, “mayo”, “junio”, “julio”, “agosto”, “septiembre”, “octubre”, “noviembre”, “diciembre”}; Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 5

OUTPUT WITH printf • printf(“Topic: %sn”, string_var); • printf(“***%8 s***%3 s***n”, “Short”, “Strings”); –

OUTPUT WITH printf • printf(“Topic: %sn”, string_var); • printf(“***%8 s***%3 s***n”, “Short”, “Strings”); – The first string is displayed right-justified in a field of eight columns. – The second string is longer than the specified field width, so the field is expanded to accommodate it exactly with no padding. • printf(“%-20 sn”, president); – Placing a minus sign prefix on a placeholder’s field width causes left justification of the value displayed. Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6

INPUT WITH scanf( ) • char dept[STRING_LEN]; • scanf(“%s”, dept); – Array output arguments

INPUT WITH scanf( ) • char dept[STRING_LEN]; • scanf(“%s”, dept); – Array output arguments are always passed to functions by sending the address of the initial array element – scanf(“%s”, ) skips leading whitespace characters such as blanks, newlines, and tabs – When it comes across a whitespace character, scanning stops Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 7

Figure 9. 2 String Input/Output with scanf and printf Copyright © 2004 Pearson Addison-Wesley.

Figure 9. 2 String Input/Output with scanf and printf Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 8

Figure 9. 3 Execution of scanf ("%s", dept); Copyright © 2004 Pearson Addison-Wesley. All

Figure 9. 3 Execution of scanf ("%s", dept); Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 9

Figure 9. 4 Execution of scanf("%s%d", dept, &course_num, days, &time); on Entry of Invalid

Figure 9. 4 Execution of scanf("%s%d", dept, &course_num, days, &time); on Entry of Invalid Data Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 10

9. 2 STRING LIBRARY FUNCTIONS: ASSIGNMENT AND SUBSTRINGS • Array name with no subscript

9. 2 STRING LIBRARY FUNCTIONS: ASSIGNMENT AND SUBSTRINGS • Array name with no subscript represents the address of the initial array element char one_str[20]; one_str = “Test string”; /* Does not work */ • Function strcpy copies the string that is its second argument into its first argument. • The return value of most string functions is char *, the returned string Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11

String Library Functions: Assignment and Substrings (cont’d) • Function strncpy takes an argument specifying

String Library Functions: Assignment and Substrings (cont’d) • Function strncpy takes an argument specifying the number of characters to copy. • For example: strncpy(one_str, "Test string", 20); would give one_str the value: Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 12

Substrings • A fragment of a longer string • For example – Assuming the

Substrings • A fragment of a longer string • For example – Assuming the prototype of strncpy is • char *strncpy (char *dest, const char *source, size_t n); – The code fragment • char result[10], sl[15] = “Jan. 30, 1996”; • strncpy (result, sl, 9); • result[9] = ‘’; Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 13

Figure 9. 5 Execution of strncpy(result, s 1, 9); result[9] = ‘�’; Copyright ©

Figure 9. 5 Execution of strncpy(result, s 1, 9); result[9] = ‘’; Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 14

Figure 9. 6 Execution of strncpy(result, &s 1[5], 2); Copyright © 2004 Pearson Addison-Wesley.

Figure 9. 6 Execution of strncpy(result, &s 1[5], 2); Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 15

EXAMPLE 9. 3 • (Example 9. 2 自己看&試試, 擷取切割子字串) – strtok() • The program

EXAMPLE 9. 3 • (Example 9. 2 自己看&試試, 擷取切割子字串) – strtok() • The program in Fig. 9. 7 breaks compounds into their elemental components, assuming that each element name begins with a capital letter and that our implementation is using the ASCII character set. Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 16

 • string length- in a character array, the number of characters before the

• string length- in a character array, the number of characters before the first null character – As found by strlen( ) – The type of the value returned is size_t, an unsigned integer type • empty string- a string of length zero Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 17

Figure 9. 7 Program Using strncpy and strcpy Functions to Separate Compounds into Elemental

Figure 9. 7 Program Using strncpy and strcpy Functions to Separate Compounds into Elemental Components Na 2 S 2 O 3 硫代硫酸鈉 Na. Cl H 2 SO 4 H 2 O Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 18

9. 3 LONGER STRINGS: CONCATENATION AND WHOLE-LINE INPUT • Concatenation – Joining of two

9. 3 LONGER STRINGS: CONCATENATION AND WHOLE-LINE INPUT • Concatenation – Joining of two strings – String library functions strcat and strncat modify their first string argument by adding all or part of their second string argument at the end of the first argument. – The strncat() function appends not more than “third argument” characters from “second argument”, and then adds a terminating `'. • 跟strncpy不太一樣 Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 19

Distinction Between Characters and Strings • Note the difference internally between the representations of

Distinction Between Characters and Strings • Note the difference internally between the representations of the character 'Q' and the string "Q". • If you wish to add a single character at the end, you should view the string as an array and assign to the end, including the null character Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 20

Scanning a Full Line • gets – Interactive input of one complete line of

Scanning a Full Line • gets – Interactive input of one complete line of data • Consider this code fragment: char line[80]; printf("Type in a line of data. n> "); gets(line); • The value stored in line would be • The n character representing the <return> or <enter> key pressed at the end of the sentence is not stored when using gets( ). Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 21

SCANNING A FULL LINE • fgets – Takes three arguments • Output parameter string

SCANNING A FULL LINE • fgets – Takes three arguments • Output parameter string • Maximum number of characters to store (n) – Never store more than n-1 characters – Final character stored will always be ‘’ • File pointer to the data source – If it has room to store entire line, it will include ‘n’ before ‘’, which is different from gets( ) – Return 0 means it encounters the end of file – gets( ) is unsafe. Some compilers will warn you Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 22

Figure 9. 8 Demonstration of Whole-Line Input 自己看… Copyright © 2004 Pearson Addison-Wesley. All

Figure 9. 8 Demonstration of Whole-Line Input 自己看… Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 23

Figure 9. 8 Demonstration of Whole-Line Input Copyright © 2004 Pearson Addison-Wesley. All rights

Figure 9. 8 Demonstration of Whole-Line Input Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 24

9. 4 STRING COMPARISON • str 1 < str 2 ? ? No! •

9. 4 STRING COMPARISON • str 1 < str 2 ? ? No! • The standard string library provides the int function strcmp for comparison of two strings. • strcmp seperates its argument pairs into three categories. TABLE 9. 2 Possible Results of strcmp(str 1, str 2) Relationship Value returned Example str 1 is less than str 2 Negative integer str 1 is “marigold” str 2 is “tulip” str 1 equals str 2 Zero Str 1 and Str 2 are both “end” str 1 is greater than str 2 Positive integer Str 1 is “shrimp” Str 2 is “crab” Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 25

String Comparison Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 26

String Comparison Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 26

EXAMPLE 9. 5 • String comparisons are essential when alphabetizing a list. • To

EXAMPLE 9. 5 • String comparisons are essential when alphabetizing a list. • To alphabetize a list of words made up of either all uppercase or all lowercase letters, we could use the selection sort algorithm. • Figure 9. 9 compares the numeric and string versions of code that compares list elements in the search for the index of the smallest. • It also shows the numeric and string versions of a code fragment that exchanges two array elements. Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 27

Figure 9. 9 Numeric and String Versions of Portions of Selection Sort That Compare

Figure 9. 9 Numeric and String Versions of Portions of Selection Sort That Compare and Exchange Elements Fig 08. 17 Copyright © 2004 Pearson Addison-Wesley. All rights reserved. Any better codes? 28

9. 5 ARRAYS OF POINTERS Figure 9. 11 Exchanging String Elements of an Array

9. 5 ARRAYS OF POINTERS Figure 9. 11 Exchanging String Elements of an Array Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 29

Figure 9. 12 Executing strcpy (list [index_of_min], list[fill]); • String is an array of

Figure 9. 12 Executing strcpy (list [index_of_min], list[fill]); • String is an array of chars • C represents every array by its starting address – list[0], …, list[4] are addresses • 換一種想法與作法 – Array of pointers Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 30

Figure 9. 13 An Array of Pointers • char *alphap[5] alphap[? ] 跟 original[?

Figure 9. 13 An Array of Pointers • char *alphap[5] alphap[? ] 跟 original[? ] • Is just as legitimate an argument as original[i] • Using array of pointers represent an ordering – Pointer requires less space than a whole string – Executes faster – Spelling correction made in original will affect all Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 31

EXAMPLE 9. 7 • The Open School admits children to its kindergarten in the

EXAMPLE 9. 7 • The Open School admits children to its kindergarten in the order in which they apply. However, most of the staff’s use of applicants is made easier if the list is alphabetized. • The program in Fig. 9. 14 takes an input list of names reflecting the order in which applications were received and creates an array of pointers through which the list can be accessed in alphabetical order. Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 32

Figure 9. 14 Two Orderings of One List Using an Array of Pointers Copyright

Figure 9. 14 Two Orderings of One List Using an Array of Pointers Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 33

Figure 9. 14 Two Orderings of One List Using an Array of Pointers (cont’d)

Figure 9. 14 Two Orderings of One List Using an Array of Pointers (cont’d) Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 34

Figure 9. 14 Two Orderings of One List Using an Array of Pointers (cont’d)

Figure 9. 14 Two Orderings of One List Using an Array of Pointers (cont’d) 要來玩玩qsort( )嗎? (Review of function pointers) Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 35

ARRAYS OF STRING CONSTANTS • C permits the use of an array of pointers

ARRAYS OF STRING CONSTANTS • C permits the use of an array of pointers to present a list of string constants. • For example – char month[12][10]={“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”}; – char *month[12]={“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”}; – 指標與陣列的曖昧關係… Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 36

9. 6 CHARACTER OPERATIONS • C provides character input/output routines as part of the

9. 6 CHARACTER OPERATIONS • C provides character input/output routines as part of the stdio library. • An extensive collection of facilities for character analysis and conversion is available in the library we #include as <ctype. h> 類似scanf(“%c”, &ch) • int getchar ( ) – Get the next character from the standard input source – Take no arguments and return the character as its result – Return EOF if encounter the end of file • int getc (FILE *inp) putchar( ) & putc( ) for char output – Get a single character from a file – The character returned is obtained from the file accessed by file pointer inp Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 37

EXAMPLE 9. 8 • In fig. 9. 15 a scanline function uses getchar. •

EXAMPLE 9. 8 • In fig. 9. 15 a scanline function uses getchar. • scanline function – Take as its first argument the string variable in which to store the input line – Take a second argument indicating the amount of space available – Store in the output argument either the full input line or as much as will fit Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 38

Figure 9. 15 Implementation of scanline Function Using getchar Copyright © 2004 Pearson Addison-Wesley.

Figure 9. 15 Implementation of scanline Function Using getchar Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 39

CHARACTER ANALYSIS AND CONVERSION Facility Checks Example isalpha If argument is a letter of

CHARACTER ANALYSIS AND CONVERSION Facility Checks Example isalpha If argument is a letter of the alphabet If(isalpha(ch)) printf(%c is a lettern“, ch); isdigit If argument is one of the dec_digit=isdigit(ch); ten decimal digits islower If argument is a (isupper) lowercase (or uppercase) letter of the alphabet If(islower(fst_let)){ printf(“n. Error: sentence”); printf(“should begin with a”); printf(“capital letter. n”) } ispunct If argument is a punctuation character If(ispunct(ch)) printf(“Punctuation mark: %cn”, ch) isspace If argument is a whitespace character c=getchar(); while(isspace(c) && c!=EOF) c=getchar(); Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 40

TABLE 9. 3 Character Classification and Conversion Facilities in ctype Library Facility tolower (toupper)

TABLE 9. 3 Character Classification and Conversion Facilities in ctype Library Facility tolower (toupper) Converts Its lowercase (or uppercase) letter argument to the uppercase (or lowercase) equivalent and returns this equivalent as the value of the call Copyright © 2004 Pearson Addison-Wesley. All rights reserved. Example if (islower(ch)) printf(“Capital %c=%cn”, ch, toupper(ch)) 41

EXAMPLE 9. 9 (自行參閱, 自修) • Figure 9. 16 shows a function string_greater that

EXAMPLE 9. 9 (自行參閱, 自修) • Figure 9. 16 shows a function string_greater that could be used to find out-of-order elements when alphabetizing a list of strings in a situation in which the case of the letters should be ignored. Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 42

Figure 9. 16 String Function for a Greater. Than Operator That Ignores Case Copyright

Figure 9. 16 String Function for a Greater. Than Operator That Ignores Case Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 43

Figure 9. 16 String Function for a Greater. Than Operator That Ignores Case (cont’d)

Figure 9. 16 String Function for a Greater. Than Operator That Ignores Case (cont’d) Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 44

9. 7 STRING-TO-NUMBER AND NUMBER-TO STRING CONVERSIONS • sprintf – Require space for a

9. 7 STRING-TO-NUMBER AND NUMBER-TO STRING CONVERSIONS • sprintf – Require space for a string as its first argument – Substitute values for placeholders, instead of printing the result, sprintf stores it in the character array accessed by its initial argument • sscanf – Take data from the string that is its first argument Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 45

Review of Use of scanf Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 46

Review of Use of scanf Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 46

Placeholders Used with printf Max width Min width %3. 3 s Copyright © 2004

Placeholders Used with printf Max width Min width %3. 3 s Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 47

Figure 9. 18 Functions That Convert Representations of Dates (直接看程式) Copyright © 2004 Pearson

Figure 9. 18 Functions That Convert Representations of Dates (直接看程式) Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 48

Figure 9. 18 Functions That Convert Representations of Dates (cont’d) Copyright © 2004 Pearson

Figure 9. 18 Functions That Convert Representations of Dates (cont’d) Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 49

Figure 9. 18 Functions That Convert Representations of Dates (cont’d) Copyright © 2004 Pearson

Figure 9. 18 Functions That Convert Representations of Dates (cont’d) Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 50

Figure 9. 18 Functions That Convert Representations of Dates (cont’d) Copyright © 2004 Pearson

Figure 9. 18 Functions That Convert Representations of Dates (cont’d) Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 51

9. 8 STRING PROCESSING ILLUSTRATED CASE STUDY: TEXT EDITOR(1/5) (自修) Step 1: Problem –

9. 8 STRING PROCESSING ILLUSTRATED CASE STUDY: TEXT EDITOR(1/5) (自修) Step 1: Problem – Design and implement a program to perform editing operations on a line of text. – Your editor should be able to locate a specified target substring, delete a substring, and insert a substring at a specified location. – The editor should expect source strings of less than 80 characters. Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 52

CASE STUDY: TEXT EDITOR(2/5) Step 2: Analysis – Problem Constant • MAX_LEN 100 –

CASE STUDY: TEXT EDITOR(2/5) Step 2: Analysis – Problem Constant • MAX_LEN 100 – Problem Inputs • char source [MAX_LEN] • char command – Problem Outputs • char source [MAX_LEN] Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 53

CASE STUDY: TEXT EDITOR(3/5) Step 3: Design – Initial Algorithm 1. Scan the string

CASE STUDY: TEXT EDITOR(3/5) Step 3: Design – Initial Algorithm 1. Scan the string to be edited into source 2. Get an edit command 3. While command isn’t Q 4. Perform edit operation 5. Get an edit command – Refinement And Program Structure Local Variables char str[MAX_LEN] int index Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 54

CASE STUDY: TEXT EDITOR(4/5) Step 3: Design – Algorithm For Do_Edit 1. switch command

CASE STUDY: TEXT EDITOR(4/5) Step 3: Design – Algorithm For Do_Edit 1. switch command ‘D’ : 2. Get the substring to be deleted (str) 3. Find the position of str in source 4. if str is found, delete it ‘I’ : 5. Get the substring to insert (str) 6. Get position of insertion (index) 7. Perform insertion of str at index position of source ‘F’ 8. Get the substring to search for (str) 9. Find the position of str in source 10. Report position otherwise: 11. Display error message Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 55

Figure 9. 19 Structure Chart for Text Editor Program Copyright © 2004 Pearson Addison-Wesley.

Figure 9. 19 Structure Chart for Text Editor Program Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 56

CASE STUDY: TEXT EDITOR(5/5) Step 4: Implementation (Figure 9. 20、Figure 9. 21) Step 5:

CASE STUDY: TEXT EDITOR(5/5) Step 4: Implementation (Figure 9. 20、Figure 9. 21) Step 5: Testing Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 57

Figure 9. 20 Text Editor Program (自修) Copyright © 2004 Pearson Addison-Wesley. All rights

Figure 9. 20 Text Editor Program (自修) Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 58

Figure 9. 20 Text Editor Program (cont’d) Copyright © 2004 Pearson Addison-Wesley. All rights

Figure 9. 20 Text Editor Program (cont’d) Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 59

Figure 9. 20 Text Editor Program (cont’d) Copyright © 2004 Pearson Addison-Wesley. All rights

Figure 9. 20 Text Editor Program (cont’d) Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 60

Figure 9. 20 Text Editor Program (cont’d) Copyright © 2004 Pearson Addison-Wesley. All rights

Figure 9. 20 Text Editor Program (cont’d) Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 61

Figure 9. 20 Text Editor Program (cont’d) Copyright © 2004 Pearson Addison-Wesley. All rights

Figure 9. 20 Text Editor Program (cont’d) Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 62

Figure 9. 20 Text Editor Program (cont’d) Copyright © 2004 Pearson Addison-Wesley. All rights

Figure 9. 20 Text Editor Program (cont’d) Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 63

Figure 9. 21 Sample Run of Text Editor Program Copyright © 2004 Pearson Addison-Wesley.

Figure 9. 21 Sample Run of Text Editor Program Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 64

9. 9 COMMON PROGRAMMING ERRORS • Figure 9. 22 shows a poor rewrite of

9. 9 COMMON PROGRAMMING ERRORS • Figure 9. 22 shows a poor rewrite of scanline function from Fig. 9. 15. – Lifetime (comparing to scope) • An error that creeps into C programs with string use is the misuse or neglect of the & operator – 請記得, string就是array of chars, array的名字就是 0元素 的位置 • Another problem that is common with string use is the overflow of character arrays allocated for strings. • All strings end with the null character, ‘’ • 字串的比較跟指定, 請用 strcmp( ) 跟 strcpy( ) Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 65

Figure 9. 22 Flawed scanline Returns Address of Deallocated Space Copyright © 2004 Pearson

Figure 9. 22 Flawed scanline Returns Address of Deallocated Space Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 66

Chapter Review (1/2) • Strings in C are arrays of characters ended by the

Chapter Review (1/2) • Strings in C are arrays of characters ended by the null character ‘’ • Using scanf and fscanf with %s descriptors for strings seperated by whitespace • Using getchar and getc for single character input • Using printf and fprintf with %s descriptors for string output, putchar and putc do single-character input • strcpy and strncpy is used for string assignment and extraction of substrings. • strcat and strncat is used for concatenation • strlen is used for string length • strcmp and strncmp is used for alphabetic comparison Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 67

Chapter Review (2/2) • The standard I/O library includes functions for string-to-number (sscanf) and

Chapter Review (2/2) • The standard I/O library includes functions for string-to-number (sscanf) and number-to-string (sprintf) conversion. • String-building functions typically require the calling module to provide space for the new string as an output parameter, and they return the address of this parameter as the function result. • The ctype library provides functions for classification and conversion of single characters. Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 68

Question? Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 69

Question? Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 69

Programming Projects 2 • A resistor is a circuit device designed to have a

Programming Projects 2 • A resistor is a circuit device designed to have a specific resistance value between its ends. Resistance values are expressed in ohms or kilo-ohms. Resistors are frequently marked with colored bands that encode their resistance values, as shown in Fig. 9. 23. The first two bands are digits, and the third is a power-of-ten multiplier. Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 70

Figure 9. 23 Bands Encoding the Resistance Value of a Resistor Copyright © 2004

Figure 9. 23 Bands Encoding the Resistance Value of a Resistor Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 71

Curses library • man curses – cc [ flag. . . ] file. .

Curses library • man curses – cc [ flag. . . ] file. . . - lcurses [ library. . . ] – #include <curses. h> • An example… #include<curses. h> main(){ int key; initscr(); cbreak(); noecho(); move(10, 0); printw("123 == %dn", 123); refresh(); while( (key=getch()) != 27 /* ESC */) { printw("[%c]", key); refresh(); } } Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 72