Presentation is loading. Please wait.

Presentation is loading. Please wait.

PRACTICAL DATA STRUCTURES USING C/C++ Chapter 2 Strings Tel: 04-7232105 ext.3242 彰師大 數學系 蔡政容 (Cheng-Jung Tsai)

Similar presentations


Presentation on theme: "PRACTICAL DATA STRUCTURES USING C/C++ Chapter 2 Strings Tel: 04-7232105 ext.3242 彰師大 數學系 蔡政容 (Cheng-Jung Tsai)"— Presentation transcript:

1 PRACTICAL DATA STRUCTURES USING C/C++ Chapter 2 Strings Email: cjtsai@cc.ncue.edu.tw Tel: 04-7232105 ext.3242 彰師大 數學系 蔡政容 (Cheng-Jung Tsai)

2 2 Outline  Introduction  2.1 characters and strings  2.2 initializing strings  2.3 passing strings between functions  2.4 working with string elements  2.5 string handling functions  2.6 sorting and processing strings  2.7 application program: text formatter

3 3 Introduction  Understand the relationships among characters, pointers, and strings. sorting.  Will help you in developing programs that are capable of many powerful operations, such as sorting.

4 4 2.1 characters and strings: Sorting strings character  A character can be thought of as a single memory location string  A string is nothing more than an arrangement of characters  See Fig. 2.1 in pages 19 NULL character (\0)  All character strings require the NULL character (\0) to let the complier know where the string ends  See Program 2.1 in pages 18  See Program 2.2 in pages 19

5 5 Where are the elements? a single memory location of one byte  In a string array, each element is a character and represents a single memory location of one byte  Pointer:  See Program 2.3 in page 20  *(ptr+1) vs. ptr+1: content+1 vs address+1  See Fig 2.3 in page 22  See Program 2.4 in page 22

6 6 2.2 initializing strings  Examine a number of ways a string of character may be initialized  char string[ ] = “hello”; Automatically reserves the correct number of memory locations, Including the final NULL character  char string[ ] = {‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’, }; The same result to above, but is a stupid initilaization  char string[5] = “hello”; Use when it is necessary to specify the length of a string  Specifying more characters for the string than are required is common  char string[80] = “hello”;  ‘x’ vs. “x” :  character vs string  “x” = ‘x’ and ‘\0’

7 7 initializing strings  Another way to initialize a string is to reserve storage for it and then use built-in functions (in ) to read the string in  Two function to read the string  scanf(): terminates scanning when it encounters the blank character  gests():  See pages 24 to 25

8 8 2.3 passing strings between functions  Strings are nothing more than character arrays,  You can pass strings between functions by passing only a pointer to the first element of the string  See Program 2.5 and 2.6 in page 26  Passing string between function1() and main() in Pro. 2.5  Passing starting address of the string between function1() and main() in Pro. 2.6  See Program 2.7 in page 27  compare the length of two strings

9 9 2.4 working with string element  This section examines a number of built-in functions that allow us to input string elements, examine them, and even convert them from one form to another  getchar() See Program 2.8 in page 28  See Program 2.8 in page 28 See Program 2.9 in pages 28 to 29  See Program 2.9 in pages 28 to 29

10 10 working with string element Checking characters  Built-in functions that will check the type of character that is entered into a program  See Table 2.1 in page 30  example: isalpha() See Program 2.10 in page 29  See Program 2.10 in page 29

11 11 Program 2.11 in page 30  Asks for a string of text from the user and prints out the string minus any punctuation or numerical digits  Lowercase alphabetical character are converted to uppercase as well  Bulit-in functions used: ispunct() & isdigit() & islower() & 0xdf  string[i] = string[i] & 0xdf

12 12 ASCII Table

13 13

14 14

15 15 Lowercase  uppercase & 0xdf  string[i] = string[i] & 0xdf  0x: 16 進位  df: 1101 1111  a:61, A: 41  a:61 hex, A: 41 hex : 0110 0001  a  (61) hex : 0110 0001 and and 1101 1111 0100 0001  41

16 16 Program 2.12 in page 31  Asks user to enter a signed integer and then proceeds to examine the input string and build a resulting integer value based on the ASCII characters entered.  number += string[i] – 0x30  0x30: (30) hex  0  Ex. (36) hex -(30) hex = 6  6-0  See Program 2.13:  See Program 2.13: Using built-in function atoi() when you are codeing your program  atof( )  ASCII to flot  atol( )  ASCII to long

17 17 2.5 String Handling Functions  Examples covered so far have dealt with operations on character strings on a character-by-character basis  What are the operations we might need to perform on an entire string?  Find the length of a string, combine two strings, compare two strings, search a string for a character...etc  Are provided in  See Table 2.2 in page 33  See Table 2.2 in page 33 to see a subset of the available string handling functions

18 18 String Handling Functions: e strlen(), strcat() and strncat  strlen()  Return the length of a string, but not including the NULL  See Program 2.14 in page 34  Implement strlen() function by your self code See Program 2.15 in page 35 See Program 2.15 in page 35  strcat(str1,str2) and strncat(str1,str2,n) See Fig 2.4  Concatenate two strings, See Fig 2.4  See Program 2.16 in page 36  Implement strcat() function by your self code See Program 2.17 in page 36 See Program 2.17 in page 36

19 19 String Handling Functions: strcmp() and strncmp()  strcmp() and strncmp()  Compare two strings Return 0 if they are identical <0 if the first string precedes the second >1 otherwise  See page 37 for examples Doe, Morries, Morrison, Smith See Program 2.18 in page 37  See Program 2.18 in page 37  Implement strcmp() function by your self code See Program 2.19 in page 38 See Program 2.19 in page 38 length = (a > b) ? a : b If a > b then a else b

20 20 String Handling Functions: strchr() and strrchr()  strchr(str1,str2) and strrchr(str1,str2)  Search a string for a specified character  Two arguments The first is the string to be searched The second is the character to search for  Both return a pointer to the character’s position within the string if found, or NULL if not found  strchr() return the position of the first occurrence  strrchr() return the position of the last occurrence  See Program 2.20 in page 40 for an example of strchr()

21 21 String Handling Functions: strstr() and strpbrk()  strstr(str1,str2)  Searches for the first occurrence of a substring within a string  return a pointer to the beginning of the substring  NULL if not found  See Program 2.21 in page 41 for an example  strpbrk(str1,str2)  Searches for the first occurrence of any character of a substring within a string  strpbrk(“good morning”,”time out”) Will return the pointer to the first ‘o’ in “good morning”  See Program 2.22 in page 42 for an example

22 22 String Handling Functions: strtod(), strtol() and strtoul()  Convert strings into double, long, unsigned long

23 23 2.6 sorting and processing strings  See Program 2.23 in page 43  Sort the list of names  Two-dimensional array is used This initialization in this example will automatically fills the remaining character position with NULL bubble sort in page 44 (see next slide for example)  Using bubble sort in page 44 (see next slide for example) Simple but inefficient (O(n 2 ))  Bulit-in function strcpy() is used  See Program 2.24 in page 44 ragged array  Similar to Program 2.23 but different definition of array (ragged array) is used (see page 46  ragged array is a more efficient storage method than rectangular array but is also harder to work with (see page 46)

24 24 Bubble sort  Original string  String after first loop

25 25 ISBN checker  See Program 2.25 in page 47  Checks a user-supplied ISBN code to determine if it is a valid sequence  See page 48  See page 48 for the format of an legal ISBN code  Note two statements total += (input[i] - 0x30) * (i + 1); printf(“c%”, (rem + 0x30) )

26 26 Vowel counter  See Program 2.26 in page 49  To reduce the number of comparisons required, each character from the input string is converted into uppercase if (isalpha(text[j]) != 0) & 0xdf text[j] = text[j] & 0xdf; /* Uppercase is still uppercase, but lowercase will become uppercase, see slide 15 */

27 27 Palindrome checker palindrome  A palindrome is a string of symbols that reads the same forward and backward  mom, otto, 11011011  Palindrome play an important role in the study of languages  See Program 2.27 in page 50  Note the use of two variables “lchar” and “rchar”  Note the use of variable “stopped “

28 28 A tokenizer tokenizer token  A tokenizer in complier breaks each line of source file into smallest parts of a language (token).  See Fig 2.7 in page 51  See Fig 2.7 in page 51 to understand the structure of a complier  See Program 2.28 in page 52 to understand the code of a limited tokenizer

29 29 Encoding text transposition encoding  One of the simplest technique is called transposition encoding  The input text is written as a two-dimensional array of characters  Then the array is transposed  Ex. In p.54 “c programming is fun”   Transposed array  output is “crunpagnrmiomsgif”  See Program 2.29 in page 55,  See Program 2.29 in page 55, please note that  variable i : the number of blanks, n: used to created a n*n array c p r o g r a m m i n g i s f u n c r n u p a g h r m i o m s g i f

30 30 2.7 application program: text formatter inserting the correct number of blanks between wordsall lines exactly fit between the predefined left and right margins.  The purpose of the text formatter is to adjust the way a block of text is displayed or printed by inserting the correct number of blanks between words on any given line in such a way that all lines exactly fit between the predefined left and right margins.  See Program page 56 for examples  Two problem  How do we determine how many words will fit on a single line  Each line may require a different number of blanks to be inserted

31 31 Application program: text formatter  One possible solution:  Advance through the line of text until a blank is found and then insert a new blank  Then advance again until the end of the next word is found and insert a blank there  Repeat this process until the desired number of blanks have been inserted  See Program page 57 for example

32 32 Application program: text formatter  Developing the algorithm  See the 5 steps of the algorithm in page 58  The overall process  See the 6 steps of the detailed process of this algorithm in page 58  Four user-defined functions used in this program  initbuff(), get(), loadword(), expand_line() in page 58  See the code for main() in page 58  You are encouraged to think of a different way to achieve the same goal.


Download ppt "PRACTICAL DATA STRUCTURES USING C/C++ Chapter 2 Strings Tel: 04-7232105 ext.3242 彰師大 數學系 蔡政容 (Cheng-Jung Tsai)"

Similar presentations


Ads by Google