Presentation is loading. Please wait.

Presentation is loading. Please wait.

String C and Data Structures Baojian Hua

Similar presentations


Presentation on theme: "String C and Data Structures Baojian Hua"— Presentation transcript:

1 String C and Data Structures Baojian Hua bjhua@ustc.edu.cn

2 What ’ s a String? A string is a sequence of characters: Every character c is taken from some char table (say the ASCII or the UniCode) Ex: “hello, world”, “string1\tstring2\n” Essentially, a string is a linear list But different operations

3 Isn ’ t String a char*? C ’ s convention for string representation C has no built-in string type Every string is a char array (char *) terminated with char ‘\0’ Operations (see the library): char *strcpy (char *s, const char *ct); char *strcat (char *s, const char *ct); … Such operations are array-based and thus efficient

4 “ string ” ADT Weakness of C ’ s “ char * ” string: Some strings can not even be represented Ex: “aaa\0bbb\0c” Some operations are dangerous it’s programmers’ duty to guarantee its safety Ex: strcpy (“ab”, “1234”) some viruses take advantage this… We want an ADT “string” that: hides the concrete representation of string offers safe operations

5 Abstract Data Types in C: Interface // in file “str.h” #ifndef STR_H #define STR_H typedef struct str *str; str new (char *s); str new2 (char *s, int size); int size (str s); bool isEmpty (str s); char nth (str s, int n); str concat (str s1, str s2); str sub (str s, int i, int j); str clone (str s); #endif

6 Array-based Implementation // in file “str.c” #include “str.h” struct str { char *s; int size; }; 0 size-1 s size str

7 Operations: “ new ” str new (char *s) { int len = strlen (s); str p = checkedMalloc (sizeof (*p)); p->s = checkedMalloc (len * sizeof(char)); for (int i=0; s[i]; i++) (p->s)[i] = s[i]; p->size = len; return p; }

8 Operations: “ new ” str new (char *s) { int len = strlen (s); str p = checkedMalloc (sizeof (*p)); p->s = checkedMalloc (len * sizeof(char)); for (int i=0; s[i]; i++) (p->s)[i] = s[i]; p->size = len; return p; } $%^& @#$% p

9 Operations: “ new ” str new (char *s) { int len = strlen (s); str p = checkedMalloc (sizeof (*p)); p->s = checkedMalloc (len * sizeof(char)); for (int i=0; s[i]; i++) (p->s)[i] = s[i]; p->size = len; return p; } 0 size-1 s @#$% p

10 Operations: “ new ” str new (char *s) { int len = strlen (s); str p = checkedMalloc (sizeof (*p)); p->s = checkedMalloc (len * sizeof(char)); for (int i=0; s[i]; i++) (p->s)[i] = s[i]; p->size = len; return p; } 0 size-1 s @#$% p

11 Operations: “ new ” str new (char *s) { int len = strlen (s); str p = checkedMalloc (sizeof (*p)); p->s = checkedMalloc (len * sizeof(char)); for (int i=0; s[i]; i++) (p->s)[i] = s[i]; p->size = len; return p; } 0 size-1 s size p

12 Operations: “ size ” int size (str s) { return s->size; } 0 size-1 s size p

13 Operations: “ nth ” char nth (str s, int n) { if (n =size(s)) error (“invalid index”); return (s->s)[n]; } 0 size-1 s size s

14 Operations: “ concat ” str concat (str s1, str s2) { int n1 = size (s1); int n2 = size (s2); str p = checkedMalloc (sizeof (*p)); p->s = checkedMalloc ((n1+n2) * sizeof(char)); for (int i=0; i<n1; i++) (p->s)[i] = nth (s1, i); for (int j=0; j<n2; j++) (p->s)[n1+j] = nth (s2, j); p->size = n1+n2; return p; }

15 Operations: “ concat ” s size s1 s size s2 s size p

16 Operations: “ clone ” str clone (str s) { int n = size (s); str p = checkedMalloc (sizeof (*p)); p->s = checkedMalloc (n * sizeof(char)); for (int i=0; i<n; i++) (p->s)[i] = nth (s, i); p->size = n; return p; }

17 Operations: “ clone ” s size s s q

18 Operations: “ sub ” str sub (str s, int from, int to) { int n = size (s); if (from>to || from =n) error (“invalid index”); str p = checkedMalloc (sizeof (*p)); p->s = checkedMalloc ((to-from+1)*sizeof(char)); for (int i=from; i<to; i++) (p->s)[i-from] = nth (s, i); p->size = to-from+1; return p; }

19 Operations: “ clone ” s size s s q fromto

20 Summary The string representation discussed in this class is functional functional: data never change, instead, we always make new data from older ones Java and ML also have functional strings In general, functional data structures are easy to implement, maintain and reason about and thus have much to be recommended


Download ppt "String C and Data Structures Baojian Hua"

Similar presentations


Ads by Google