Strings Jarret Raim 2002. C Strings Same as arrays of characters. Use pointers. Require static declarations (compile time). No bounds checking. No easy.

Slides:



Advertisements
Similar presentations
C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void.
Advertisements

Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
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.
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.
Lecture 24: Strings. 2 Lecture Contents: t Library functions t Assignment and substrings t Concatenation t Comparison t Demo programs t Exercises.
Introduction to the STL
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
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.
CS 31 Discussion, Week 6 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Standard Template Library C++ introduced both object-oriented ideas, as well as templates to C Templates are ways to write general code around objects.
Introduction to C programming
Chapter 8 Strings and Vectors (8.1 and 8.2). An Array of characters Defined as: char firstName[20]; char firstName[] = {‘T’, ‘i’, ‘m’}; // an array of.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
CSE 232: C++ pointers, arrays, and references Overview of References and Pointers Often need to refer to another object –Without making a copy of the object.
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Dynamic memory allocation and Pointers Lecture 4.
COP 3530 Data Structures & Algorithms Discussion Session 3.
9-1 Learning Objectives  An Array Type for Strings  C-Strings.
Exam Format  90 Total Points  60 Points Writing Programs  25 Points Tracing Code/Algorithms and determining results  5 Points Short Answer  Similar.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
String Class. C-style and C++ string Classes C-style strings, called C-strings, consist of characters stored in an array ( we’ll look at them later) C++
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character.
STL string class Programming Team 2/15/2006. string constructors string() = empty string string(int l, char ch) = string of length l, of repeated ch string(char*
1 Object-Oriented Programming Using C++ A tutorial for pointers.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.
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.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
 Learn how to form strings using one-dimensional array  String manipulation functions:  strcpy  strrev  strcmp  Program using strings.
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
“Generic Programming” ECE 297
Pointers and Dynamic Arrays
Motivation and Overview
Object Oriented Programming COP3330 / CGS5409
CS 2308 Exam I Review.
CS 2308 Exam I Review.
Pointers, Dynamic Data, and Reference Types
Introduction to Programming
Arrays and Pointers Reference: Chapter , 4.11 CMSC 202.
Programming with ANSI C ++
CMSC 202 Lesson 22 Templates I.
Chapter 9 One-Dimensional Arrays
C++ Pointers and Strings
Templates I CMSC 202.
Today’s Objectives 28-Jun-2006 Announcements
CS31 Discussion 1H Fall18: week 6
An Introduction to STL.
C++ Pointers and Strings
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

Strings Jarret Raim 2002

C Strings Same as arrays of characters. Use pointers. Require static declarations (compile time). No bounds checking. No easy way to increase / decrease sizes. Every string must have a delimeter on the end (‘\0’). Must use inefficient functions from C libraries to act on the strings. Very bad errors.

Examples 1. Static Declaration char myString[20]; Only possible to hold 19 characters, not 20. Can’t be enlarged or shrunk. 2. Dynamic Declaration char *myString = new char[ 20 ]; Still only holds 19 characters! Can’t easily be enlarged or shrunk.

Standard Template Library Developed by SGI. Contains many improved data structures. – vectors, strings, ropes, lists, deques, etc. Comes included with most (all?) C++ distributions. Easier to use More portable Faster More functionality Analogous to the Java String class

STL: String example string myString; myString = “Hello World!”; cout << myString << endl;

Comparison: String Concatenation char myString[10]; char anotherString[10]; char newString[20]; myString = “foo”; anotherString = “barstool”; newString = Strcat( myString, anotherString ); returns -> “foobarstool” ? Nope, crashes because myString isn’t big enough to hold “foo” + “barstool”.

Comparison: String Concatenation string myString = “times”; string anotherString = “meacupla”; string newString = “”; newString = myString + anotherString; cout << newString << endl; returns -> “timesmeaculpa”

Comparison: Substrings char c[10] = “this is a “; char subStr[3]; for( int i = 0; i < 2; i++ ) subStr[i] = c[i+1]; cout << subStr << endl; Returns -> ‘hi’

Comparison: Substrings string myString = “this is a time for all good”; string temp; temp = myString.substr( 1, 4 ); cout << temp << endl; returns -> ‘his ‘

Comparison: Comparison char temp[10] = “this is”; char t1[10] = “not equal”; if( temp[10] == t1[10] ) cout << “They are the same!” << endl; else cout << “Nope, not the same!” << endl; Returns -> “Nope, not the same!” WRONG!

Comparison: Comparison string myString = “now is the”; string newString = “time”; if( myString == newString ) cout << “They are the same!” << endl; else cout << “Nope, not the same!” << endl; Returns -> “Nope, not the same!”

Note Most of the functionality in the STL has been written for C strings. They are usually very hard to understand: void *memmove(void *s1, const void *s2, size_t n); void *memset(void *s, int c, size_t n); char *strcat(char *s1, const char *s2); char *strchr(const char *s, int c); [not in C++] const char *strchr(const char *s, int c); [C++ only] char *strchr(char *s, int c); [C++ only] int strcmp(const char *s1, const char *s2);memmovememsetstrcatstrchr strcmp

Rules for using stl::strings No need to grow or shrink a string, just use assignment (=). Lots of pre-made functions – Searching, sorting, substrings, copying, etc. Easy to get the size of the string Easy to insert and delete parts of the string

Conclusions Use STL data structures as much as you can. You still have to understand the hard way to understand and use older code. Look at this website to get the code for the STL and all the documentation.