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 ci (0 ≤ i<n) is taken from some character set (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 Problems with C String? Weakness of C ’ s “ char * ” string: Strings are unchangeable See demo of C’s “char *”… Why? Some strings can not even be represented Ex: “aaa\0bbb\0c” Why?

5 Problems with C String? Some operations are dangerous: Ex: strcpy (“ab”, “1234”) Notorious source of bugs it’s programmers’ duty to prevent these Some viruses take advantage this… Robert Morris’s worm in 1988 the world’s first wide-spread See demo for this…

6 “ string ” ADT We want an ADT “string”: hides the concrete representation of string offers more flexible operations and cures security problems But to be compatible with C, ‘\0’ is not allowed in string

7 Abstract Data Types in C: Interface // in file “str.h” #ifndef STR_H #define STR_H typedef struct strStruct *str; str newStr (char *s); int size (str s); int isEmpty (str s); int nth (str s, int n); str concat (str s1, str s2); str sub (str s, int i, int j); void append (str s1, str s2); #endif

8 Array-based Implementation // in file “str.c” #include “str.h” struct strStruct { char *s; int size; }; // What’s the difference // with extensible array? 0 n s size str

9 Operations: “ new ” str newStr (char *s) { int len = strlen (s); str p = (str)malloc (sizeof (*p)); p->s = (char *)malloc ((len+1) * sizeof(char)); while (*(p->s)++ = *s++); p->size = len; return p; }

10 Operations: “ new ” str newStr (char *s) { int len = strlen (s); str p = (str)malloc (sizeof (*p)); p->s = (char *)malloc ((len+1) * sizeof(char)); while (*(p->s)++ = *s++); p->size = len; return p; } $%^& @#$% p

11 Operations: “ new ” str newStr (char *s) { int len = strlen (s); str p = (str)malloc (sizeof (*p)); p->s = (char *)malloc ((len+1) * sizeof(char)); while (*(p->s)++ = *s++); p->size = len; return p; } 0 len s @#$% p

12 Operations: “ new ” str newStr (char *s) { int len = strlen (s); str p = (str)malloc (sizeof (*p)); p->s = (char *)malloc ((len+1) * sizeof(char)); while (*(p->s)++ = *s++); p->size = len; return p; } 0 len s @#$% p

13 Operations: “ new ” str newStr (char *s) { int len = strlen (s); str p = (str)malloc (sizeof (*p)); p->s = (char *)malloc ((len+1) * sizeof(char)); while (*(p->s)++ = *s++); p->size = len; return p; } 0 len s size p

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

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

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

17 Operations: “ concat ” str concat (str s1, str s2) { int n1 = size (s1); int n2 = size (s2); str p = (str)malloc (sizeof (*p)); p->s = (char *)malloc ((n1+n2+1) *sizeof(char)); // str copy, leave to you …; p->size = n1+n2; return p; }

18 Operations: “ append ” s size s1 s size s2

19 Operations: “ append ” str concat (str s1, str s2) { int n1 = size (s1); int n2 = size (s2); char *t; t = (char *)malloc ((n1+n2+1) *sizeof(char)); // str copy, leave to you …; p->size = n1+n2; return p; }

20 Operations: “ sub ” s size s s q fromto

21 Operations: “ sub ” str sub (str s, int from, int to) { int n = size (s); if (from>to || from =n) error (“invalid index”); str p = (str)malloc (sizeof (*p)); p->s = (char *)malloc((to-from+2)*sizeof(char)); // str copy, leave to you …; p->size = to-from+1; return p; }

22 Summary The string representation discussed in this class is more functional than procedural 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