String C and Data Structures Baojian Hua

Slides:



Advertisements
Similar presentations
C and Data Structures Baojian Hua
Advertisements

C’ POINTERS Basic&Examples. Q:what’s the output? int array[] = { 45, 67, 89 }; int *array_ptr = array; printf(" first element: %i\n", *(array_ptr++));
Data Structure & Abstract Data Type
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
C Module System C and Data Structures Baojian Hua
Data Structure & Abstract Data Type C and Data Structures Baojian Hua
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
Elementary Data Structures: Part 2: Strings, 2D Arrays, Graphs
Chapter Fourteen Strings Revisited. Strings A string is an array of characters A string is a pointer to a sequence of characters A string is a complete.
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
Lecture 24: Strings. 2 Lecture Contents: t Library functions t Assignment and substrings t Concatenation t Comparison t Demo programs t Exercises.
Extensible Array C and Data Structures Baojian Hua
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
. Plab – Tirgul 2 Const, C Strings. Pointers int main() { int i,j; int *x; // x points to an integer i = 1; x = &i; j = *x; ijx 1.
Map, Set & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Abstract Data Type C and Data Structures Baojian Hua
Queue C and Data Structures Baojian Hua
Stack C and Data Structures Baojian Hua
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Prepared by Dr. Inayatullah Shah1 Data Structures CSC212.
Relation Discrete Mathematics and Its Applications Baojian Hua
1 Data Structures CSC Data Types & Data Structures Applications/programs read, store and operate on data. Finally output results. What is data?
1 Data Structures: Introduction CSC Data Types & Data Structures Applications/programs read data, store data temporarily, process it and finally.
String C and Data Structures Baojian Hua
Pointers and Arrays C and Data Structures Baojian Hua
Graph C and Data Structures Baojian Hua
Stack C and Data Structures Baojian Hua
C and Data Structures Baojian Hua
Graph C and Data Structures Baojian Hua
Hash Discrete Mathematics and Its Applications Baojian Hua
Queue C and Data Structures Baojian Hua
Lexing Discrete Mathematics and Its Applications Baojian Hua
Extensible Array C and Data Structures Baojian Hua
Extensible Array C and Data Structures Baojian Hua
Linked List C and Data Structures Baojian Hua
Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Functional List C and Data Structures Baojian Hua
String C and Data Structures Baojian Hua
Set & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Graph Discrete Mathematics and Its Applications Baojian Hua
String What it is Why it’s useful library routines for handling strings how to input a string from the keyboard.
Hash C and Data Structure Baojian Hua
Memory Layout C and Data Structures Baojian Hua
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
Binary Search Tree C and Data Structures Baojian Hua
C-Strings Joe Meehean. C-style Strings String literals (e.g., “foo”) in C++ are stored as const char[] C-style strings characters (e.g., ‘f’) are stored.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
Introduction to Data Structures Systems Programming Concepts.
1 Data Structures and Algorithms Week 3 Data Abstraction Part I: Modules and Information Hiding.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
Hash C and Data Structure Baojian Hua
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Sahar Mosleh California State University San MarcosPage 1 Character String.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
1 C Basics. 2 The C Language Spirit Made by professional programmers for professional programmers Very flexible, very efficient, very liberal Does not.
Prepared by Dr. Inayatullah Shah
Discrete Mathematics and
Data Structures: Introduction
Data Structures: Introduction
Presentation transcript:

String C and Data Structures Baojian Hua

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

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

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?

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…

“ 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

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

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

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; }

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

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 p

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 p

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

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

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

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

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; }

Operations: “ append ” s size s1 s size s2

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; }

Operations: “ sub ” s size s s q fromto

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; }

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