PRACTICAL DATA STRUCTURES USING C/C++ Chapter 2 Strings Email: [email protected] Tel: 04-7232105...

32
PRACTICAL DATA STRUCTURES USING C/C++ Chapter 2 Strings Email: [email protected] Tel: 04-7232105 ext.3242 彰彰彰 彰彰彰 彰彰彰 (Cheng-Jung Tsai )

Transcript of PRACTICAL DATA STRUCTURES USING C/C++ Chapter 2 Strings Email: [email protected] Tel: 04-7232105...

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

PRACTICAL DATA STRUCTURES USING

C/C++

Chapter 2Strings

Email: [email protected]: 04-7232105 ext.3242

彰師大 數學系蔡政容 (Cheng-Jung Tsai)

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

2

Outline

IntroductionIntroduction 2.1 characters and strings2.1 characters and strings 2.2 initializing strings2.2 initializing strings 2.3 passing strings between functions2.3 passing strings between functions 2.4 working with string elements2.4 working with string elements 2.5 string handling functions2.5 string handling functions 2.6 sorting and processing strings2.6 sorting and processing strings 2.7 application program: text formatter2.7 application program: text formatter

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

3

Introduction

Understand the relationships among characters, Understand the relationships among characters, pointers, and strings.pointers, and strings.

Will help you in developing programs that are capable of many powerful operations, such as sorting.sorting.

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

4

2.1 characters and strings: Sorting strings

A charactercharacter can be thought of as a single memory location

A stringstring is nothing more than an arrangement of characters

See Fig. 2.1 in pages 19 All character strings require the NULL character (\0NULL 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

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

5

Where are the elements?

In a string array, each element is a character and represents a single memory location of one byte a single memory location of one byte

Pointer:Pointer: See Program 2.3 in page 20

*(ptr+1) vs. ptr+1: content+1 vs address+1*(ptr+1) vs. ptr+1: content+1 vs address+1 See Fig 2.3 in page 22 See Program 2.4 in page 22

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

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’

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

7

initializing strings

Another way to initialize a string is to reserve storage for it and then use built-in functions (in <stdio.h>) 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 25See pages 24 to 25

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

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 26See 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 See Program 2.7 in page 27

compare the length of two strings

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

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 28See Program 2.8 in page 28 See Program 2.9 in pages 28 to 29See Program 2.9 in pages 28 to 29

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

10

working with string elementChecking characters

Built-in functions that will check the type of character that is entered into a program See Table 2.1 in page 30See Table 2.1 in page 30

example: isalpha()example: isalpha() See Program 2.10 in page 29See Program 2.10 in page 29

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

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(Bulit-in functions used: ispunct() & isdigit() & islower()) string[i] = string[i] & 0xdf& 0xdf

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

12

ASCII Table

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

13

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

14

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

15

Lowercase uppercase

string[i] = string[i] & 0xdf& 0xdf 0x: 16 進位 df: 1101 1111

a:61a:61hex, A: 41, A: 41hex

a (61)hex: 0110 0001: 0110 0001

and and 1101 1111

0100 000141

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

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] – 0x30number += string[i] – 0x30 0x30: (30)hex0

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

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

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 <string.h> See Table 2.2 in page 33See Table 2.2 in page 33 to see a subset of the available

string handling functions

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

18

String Handling Functions: estrlen(), strcat() and strncat

strlen() Return the length of a string, but not including the NULL See Program 2.14 in page 34See Program 2.14 in page 34 Implement strlen() function by your self code

See Program 2.15 in page 35See Program 2.15 in page 35

strcat(str1,str2) and strncat(str1,str2,n) Concatenate two strings, See Fig 2.4See Fig 2.4 See Program 2.16 in page 36See Program 2.16 in page 36 Implement strcat() function by your self code

See Program 2.17 in page 36See Program 2.17 in page 36

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

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 examplesSee page 37 for examples Doe, Morries, Morrison, Smith

See Program 2.18 in page 37See Program 2.18 in page 37 Implement strcmp() function by your self code

See Program 2.19 in page 38See Program 2.19 in page 38 length = (a > b) ? a : b

If a > b then a else b

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

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()See Program 2.20 in page 40 for an example of strchr()

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

21

String Handling Functions: strstr() and strpbrk()

strstr(str1,str2) Searches for the first occurrence of a substring within a s

tring return a pointer to the beginning of the substring NULL if not found See Program 2.21 in page 41 for an exampleSee Program 2.21 in page 41 for an example

strpbrk(str1,str2) Searches for the first occurrence of any character of a s

ubstring 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 exampleSee Program 2.22 in page 42 for an example

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

22

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

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

23

2.6 sorting and processing strings

See Program 2.23 in page 43See 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

Using bubble sort bubble sort in page 44 (see next slide for examplein page 44 (see next slide for example)) Simple but inefficient (O(n2))

Bulit-in function strcpy() is used See Program 2.24 in page 44See Program 2.24 in page 44

Similar to Program 2.23 but different definition of array (ragged ragged arrayarray) is used

ragged array is a more efficient storage method than rectangular array but is also harder to work with (see page 46(see page 46)

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

24

Bubble sort

Original string

String after first loop

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

25

ISBN checker

See Program 2.25 in page 47See Program 2.25 in page 47 Checks a user-supplied ISBN code to determine if it is a v

alid sequence See page 48See page 48 for the format of an legal ISBN code Note two statements

total += (input[i] - 0x30) * (i + 1); printf(“c%”, (rem + 0x30) )

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

26

Vowel counter

See Program 2.26 in page 49See Program 2.26 in page 49 To reduce the number of comparisons required, each ch

aracter from the input string is converted into uppercase if (isalpha(text[j]) != 0)

text[j] = text[j] & 0xdf& 0xdf;

/* Uppercase is still uppercase, but lowercase will become uppercase, see slide

15 */

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

27

Palindrome checker

A palindromepalindrome 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 50See Program 2.27 in page 50 Note the use of two variables “lchar” and “rchar” Note the use of variable “stopped “

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

28

A tokenizer

A tokenizertokenizer in complier breaks each line of source file into smallest parts of a language (tokentoken). 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 See Program 2.28 in page 52 to understand the codto understand the cod

e of a limited tokenizere of a limited tokenizer

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

29

Encoding text One of the simplest technique is called transposition encotransposition enco

dingding The input text is written as a two-dimensional array of character

s 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 gr a m m in g i s fu n

c r n u p a g hr m i o m s g i f

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

30

2.7 application program: text formatter 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 inserting the correct number of blanks between wordswords on any given line in such a way that all lines all lines exactly fit between the predefined left and right exactly fit between the predefined left and right margins.margins.

See Program page 56 for examplesSee 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

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

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 Repeat this process until the desired number of

blanks have been insertedblanks have been inserted See Program page 57 for exampleSee Program page 57 for example

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

32

Application program: text formatter

Developing the algorithm See the 5 steps of the algorithm in page 58See the 5 steps of the algorithm in page 58

The overall process See the 6 steps of the detailed process of this algorithm See the 6 steps of the detailed process of this algorithm

in page 58in page 58 Four user-defined functions used in this program

initbuff(), get(), loadword(), expand_line() See the code for main() in page 58in page 58

You are encouraged to think of a different way to achieve the same goal.