String C and Data Structures Baojian Hua

Slides:



Advertisements
Similar presentations
C and Data Structures Baojian Hua
Advertisements

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
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
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.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
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.
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.
CS 241 Section Week #2 2/4/10. 2 Topics This Section MP1 overview Part1: Pointer manipulation Part2: Basic dictionary structure implementation Review.
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.
C For Java Programmers Tom Roeder CS sp. Why C? The language of low-level systems programming  Commonly used (legacy code)  Trades off safety.
Abstract Data Type C and Data Structures Baojian Hua
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Relation Discrete Mathematics and Its Applications Baojian Hua
C strings (Reek, Ch. 9) 1CS 3090: Safety Critical Programming in C.
String C and Data Structures Baojian Hua
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
Pointers and Arrays C and Data Structures Baojian Hua
C and Data Structures Baojian Hua
Graph C and Data Structures 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
Functional List C and Data Structures Baojian Hua
String C and Data Structures Baojian Hua
Polymorphism C and Data Structures Baojian Hua
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
C-strings Array with base type char One character per indexed variable
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.
Introduction to C programming
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
Array with base type char One character per indexed variable One extra character: '\0' Called ‘null character’ Delimiter of the string To declare a string,
CS 450 MPX P ROJECT A Quick C Review. P OINTERS AND ADDRESSES IN C Check Manual I2 from Dr. Mooney’s website for a complete review of C topics you will.
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
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.
Strings, Pointers and Tools
CMSC 104, Version 8/061L25Strings.ppt Strings Topics String Libraries String Operations Sample Program Reading Sections
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.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
1 C Basics. 2 The C Language Spirit Made by professional programmers for professional programmers Very flexible, very efficient, very liberal Does not.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Computer Organization and Design Pointers, Arrays and Strings in C
Lecture 8 String 1. Concept of strings String and pointers
Programming Languages and Paradigms
C Stuff CS 2308.
String in C++.
Chapter 8 Character Arrays and Strings
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 are available in standard library (see the library ): char *strcpy (char *s, const char *ct); char *strcat (char *s, const char *ct); … Operations are array-based and thus efficient

Problems with C String? Weakness of C ’ s “ char * ” string: Most strings are constants See demo of C’s “char *”… Why? May be space-consuming why? Operations may be too slow strcmp (char *, char *);

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… Morris’s worm in 1988 the world’s first wide-spread See demo for this…

“ String_t ” ADT We want an ADT “String_t”: hides the concrete representation of string offers more flexible operations and cures security problems To keep compatible with C, ‘\0’ is reserved as terminator

Interface // in file “string.h” #ifndef STRING_H #define STRING_H #define T String_t typedef char *T; // CDT. Why? T String_new (char *s); int String_size (T s); int String_isEmpty (T s); int String_nth (T s, int n); T String_concat (T s1, T s2); #undef T #endif

Array-based Implementation // in file “string.c” #include “string.h” // Basic idea is to heap-allocate arrays \0 0 n str

Operations: “ new ” String_t String_new (char *s) { int len = strlen (s); String_t p = malloc ((len+1) * sizeof(*p)); while (*p++ = *s++) ; return p; } \0 0 n p

Operations int String_size (String_t s) { return strlen (s); } int String_nth (String_t s, int n) { return s[n]; } // Recall the definition typdef char *String_t; // do we really need these functions? size

Operations: “ concat ” \0 s1 \0 s2 p \0

Operations: “ concat ” String_t String_concat (String_t s1, String_t s2) { int n1 = strlen (s1); int n2 = strlen (s2); Strint_t p = malloc ((n1+n2+1) *sizeof(*p)); // copy both s1 and s2 to p, leave to you …; return p; }

Summary so far The string representation discussed so far is functional style again: functional==data never change we always make new data from older ones Java and ML also have functional strings But for some operations, buffer may be used for efficiency purpose

Problem? It may be too slow: consider how to implement this: int strcmp(char *dst, char *src); It may be too space consuming: e.g., these two calls generate two strings: String_new ( “ hello ” ); So, we need a high-level “ string ” to resolve these

Interface // in file “str.h” #ifndef STR_H #define STR_H #define T Str_t typedef struct T *T; // ADT! T Str_new (char *s); int Str_size (T s); int Str_isEmpty (T s); int Str_nth (T s, int n); T Str_concat (T s1, T s2); #undef T #endif

Array-based Implementation // in file “str.c” #include “string.h” #include “str.h” struct Str_t { char *s; int size; int hashCode; // omitted for now }; // maintain an internal // cache of all “str”! List_t allStrs = 0; \0 0 n s size str

Operations: “ new ” Str_t Str_new (char *s) { // #1: search the “allStrs” list to try to find // “s” // #2: if #1 succeeds, then return the result // #3: else cook a new node, put it to “allStr” // and return it return p; } data next data next data next l …

Operations: “ new ” Str_t Str_new (char *s) { // we only write #3 here: Str_t temp = malloc (sizeof (*temp)); temp->str = s; temp->size = strlen (s)+1; List_insertHead (allStrs, temp); return p; } data next data next data next l …

Operations: “ equals ” int Str_equals (Str_t s1, Str_t s2) { return s1==s2; // Fast! } data next data next data next l …

Summary For fast string comparison and less memory usage, we modify the data structure of string a technique called memoization Interplay between DS and algorithm again