M The University Of Michigan Andrew M. Morgan EECS498-006 Lecture 14 Savitch Ch. 9.0 C++ String Data Type C-Strings.

Slides:



Advertisements
Similar presentations
M The University Of Michigan Andrew M. Morgan EECS Lecture 01 Savitch Ch. 2 C++ Basics Flow Of Control.
Advertisements

M The University Of Michigan Andrew M. Morgan EECS Lecture 25 Savitch Ch. 15 Polymorphism.
M The University Of Michigan Andrew M. Morgan EECS Lecture 09 Savitch Ch Compiling With Multiple Files Make Utility.
M The University Of Michigan Andrew M. Morgan EECS Lecture 22 Savitch Ch. 16 Intro To Standard Template Library STL Container Classes STL Iterators.
M The University Of Michigan Andrew M. Morgan Andrew M Morgan1 EECS Lecture 03 Savitch Ch. 3-4, Misc More on Functions Memory, Activation Records,
M The University Of Michigan Andrew M. Morgan EECS Lecture 06 Savitch Ch. 5 Arrays Multi-Dimensional Arrays.
M The University Of Michigan Andrew M. Morgan EECS Lecture 24 Savitch Ch. 14 Inheritance.
Savitch N/A Randomness In Programming Simulations
M The University Of Michigan Andrew M. Morgan Andrew M Morgan1 EECS Lecture 05 Savitch Ch Streams Stream States Input/Output.
M The University Of Michigan Andrew M. Morgan EECS Lecture 18 Savitch Ch. 17 Linked Data Structures.
Lecture 9: Character and String
LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
Copyright  Hannu Laine C++-programming Part 5 Strings.
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.
AU/MITM/1.6 By Mohammed A. Saleh 1. Introducing the string Class  Instead of using a character array to hold a string, you can use a type string variable.
Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type.
Objectives In this chapter, you will:
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:
C++ Data Type String A string is a sequence of characters enclosed in double quotes. Examples of strings: “Hello” “CIS 260” “Students” The empty string.
Representing Data Elements Gayatri Gopalakrishnan.
Program Design and Development
C-Strings A C-string (also called a character string) is a sequence of contiguous characters in memory terminated by the NUL character '\0'. C-strings.
Software and Software Vulnerabilities. Synopsis Array overflows Stack overflows String problems Pointer clobbering. Dynamic memory management Integer.
CS 117 Spring 2002 April 5, Exam 3 April 10 –files, arrays, strings, classes –practice exams are up –review on Monday.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Центр атестації педагогічних працівників 2014
Галактики і квазари.
Характеристика ІНДІЇ.
Процюк Н.В. вчитель початкових класів Боярської ЗОШ І – ІІІ ст №4
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
Week 7 – String. Outline Passing Array to Function Print the Array How Arrays are passed in a function call Introduction to Strings String Type Character.
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
STRINGS & STRING HANDLING FUNCTIONS STRINGS & STRING HANDLING FUNCTIONS.
EGR 2261 Unit 9 Strings and C-Strings  Read Malik, pages in Chapter 7, and pages in Chapter 8.  Homework #9 and Lab #9 due next week.
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
Type Conversions Implicit Conversion Explicit Conversion.
COP 3530 Data Structures & Algorithms Discussion Session 3.
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
Духовні символи Голосіївського району
1 CSC103: Introduction to Computer and Programming Lecture No 24.
Chapter 15 Strings as Character Arrays
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Programming Fundamentals. Today’s Lecture Multidimensional Arrays Arrays as Class Member Data Arrays of Objects C-Strings.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
ENEE150 – 0102 ANDREW GOFFIN Strings. Project 2 Flight Database 4 options:  Print flight  Print airport  Find non-stop flights  Find one-stop flights.
Streams and File Processing in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Programming Fundamentals. Today’s Lecture Array Fundamentals Arrays as Class Member Data Arrays of Objects C-Strings The Standard C++ string Class.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
ANNOUNCEMENT The missed lecture will be made up this Monday evening in the Tech PC classroom (MG51). A tentative time interval is 6:30-8:00. The exact.
INC 161 , CPE 100 Computer Programming
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Class Examples.
Wednesday 09/23/13.
Сътрудничество между полицията и другите специалисти в България
Съобщение Ръководството на НУ “Христо Ботев“ – гр. Елин Пелин
ДОБРОВОЛЕН РЕЗЕРВ НА ВЪОРЪЖЕНИТЕ СИЛИ НА РЕПУБЛИКА БЪЛГАРИЯ
Съвременни софтуерни решения
Програма за развитие на селските райони
ОПЕРАТИВНА ПРОГРАМА “АДМИНИСТРАТИВЕН КАПАЦИТЕТ”
БАЛИСТИКА НА ТЯЛО ПРИ СВОБОДНО ПАДАНЕ В ЗЕМНАТА АТМОСФЕРА
Стратегия за развитие на клъстера 2015
The .ASCII and .END Assembler Input ;Stan Warford ;January 13, 2005
Strings Skill Area 313 Part C
Presentation transcript:

M The University Of Michigan Andrew M. Morgan EECS Lecture 14 Savitch Ch. 9.0 C++ String Data Type C-Strings

M M EECS498 EECS498 Andrew M Morgan2 Intro To The Standard string Class C++ has a standard class called "string" Strings are simply a sequence of characters –Note: This is not a sufficient definition for a "C-string" –A "C-string" is an array of characters terminated by a null byte More on this later… Must #include using the standard namespace to get C++ standard string functionality –Note: This is different from #include'ing which is the header required for "C-string"s string variables are used to store names, words, phrases, etc. Can be input using ">>" and output using "<<" as other types

M M EECS498 EECS498 Andrew M Morgan3 Some string Functionality Declaring a string: –string lastName; –string firstName("Drew"); //Note: String literal enclosed in double quotes –string fullName; Assigning a string: –lastName = "Morgan"; //The usual assignment operator Appending one string on the end of another: –fullName = firstName + lastName; //Results in "DrewMorgan" –fullName = firstName + " " + lastName; //Results in "Drew Morgan" Accessing individual characters in a string: –myChar = firstName[2]; //Results in 'e' (no bounds checking) –myChar = firstName.at(2); //Results in 'e' (does bounds checking) Appending a character to the end of a string: –lastName = lastName + myChar; //Results in "Morgane" Determining number of characters in string: –myInt = firstName.length(); //Results in 4

M M EECS498 EECS498 Andrew M Morgan4 string Example #1 #include using namespace std; int main(void) { string first; string last("Morgan"); first = "Drew"; //Would be illegal for C-string cout << "Length of " << first << " is: " << first.length() << endl; cout << "Length of " << last << " is: " << last.length() << endl; first += "Morgan"; cout << "Length of " << first << " is: " << first.length() << endl; cout << "Length of " << last << " is: " << last.length() << endl; first.assign("Drew"); first.append(" "); first.append(last); cout << "Length of " << first << " is: " << first.length() << endl; cout << "Length of " << last << " is: " << last.length() << endl; return(0); } Length of Drew is: 4 Length of Morgan is: 6 Length of DrewMorgan is: 10 Length of Morgan is: 6 Length of Drew Morgan is: 11 Length of Morgan is: 6

M M EECS498 EECS498 Andrew M Morgan5 Additional string Functionality Strings can be compared with usual operators –>, >= (greater than, greater than/equal to) –<, <= (less than, less than/equal to) –== (equality) Strings also have a member function called "compare" –int string::compare(string rhs); –Return value is negative if calling string is less than rhs –Return value is positive if calling string is greater than rhs –Return value is zero if both strings are identical

M M EECS498 EECS498 Andrew M Morgan6 string Example #2 int main(void) { string s1 = "Drew"; string s3; int result; s3 = "Bob"; if (s3 < s1) cout << "oper: s3 less than s1"; if (s3 > s1) cout << "oper: s3 greater than s1"; if (s3 == s1) cout << "oper: s3 is equal to s1"; cout << endl; result = s3.compare(s1); if (result < 0) cout << "comp: s3 less than s1"; else if (result > 0) cout << "comp: s3 greater than s1"; else cout << "comp: s3 is equal to s1"; cout << endl; s3 = "Drew"; if (s3 < s1) cout << "oper: s3 less than s1"; if (s3 > s1) cout << "oper: s3 greater than s1"; if (s3 == s1) cout << "oper: s3 is equal to s1"; cout << endl; result = s3.compare(s1); if (result < 0) cout << "comp: s3 less than s1"; else if (result > 0) cout << "comp: s3 greater than s1"; else cout << "comp: s3 is equal to s1"; cout << endl; return (0); } oper: s3 less than s1 comp: s3 less than s1 oper: s3 is equal to s1 comp: s3 is equal to s1

M M EECS498 EECS498 Andrew M Morgan7 Even More string Functionality Getting a substring of a string: –string string::substr(int startPos, int length) Returns the substring starting at "startPos" with length of "length" Finding the location of a substring within a string: –int string::find(string lookFor); Returns the index where the first instance of "lookFor" was found in the string Returns "string::npos" (which is usually -1) when the substring isn't found –int string::find(string lookFor, int startFrom); Returns the index where the first instance of "lookFor" was found, starting the search at the index "startFrom", or "string::npos" when the substring isn't found Finding specific characters in a string: –int string::find_first_of(string charList, int startFrom); Returns the index of the first instance of any character in "charList", starting the search at the index "startFrom", or "string::npos" if none of the chars are found –int string::find_first_not_of(string charList, int startFrom); Returns the index of the first instance of any character NOT in "charList", starting the search at the index "startFrom", or "string::npos" if none of the chars are found

M M EECS498 EECS498 Andrew M Morgan8 string Example #3 int main() { int startPos; int len; int commaLoc; int howLoc; int loc; int spaceLoc; string myStr; string myStr2; myStr = "Hello, how are you?"; startPos = 7; len = 3; myStr2 = myStr.substr(startPos, len); cout << "Substr: " << myStr2 << endl; commaLoc = myStr.find(","); howLoc = myStr.find(myStr2); cout << "Comma: " << commaLoc; cout << " how: " << howLoc << endl; cout << "Spaces:"; spaceLoc = myStr.find(" "); while (spaceLoc != string::npos) { cout << " " << spaceLoc; spaceLoc = myStr.find(" ", spaceLoc + 1); } cout << endl; cout << "Punct and spaces:"; loc = myStr.find_first_of(",?", 0); while (loc != string::npos) { cout << " " << loc; loc = myStr.find_first_of(",?", loc + 1); } cout << endl; return (0); } Substr: how Comma: 5 how: 7 Spaces: Punct and spaces:

M M EECS498 EECS498 Andrew M Morgan9 string Class Implementation The string class uses dynamic memory allocation to be sure segmentation faults don't occur –When a string is updated such that it requires more characters than currently allocated, a new, larger array is allocated and the prior contents are copied over as necessary Since dynamic allocation is relatively slow, it is not desirable to be re-allocating strings often –C++ allows some memory to be "wasted" by often allocating more space than is really needed –However, as strings are appended to the end, it is likely that a re- allocation won't be needed every time –Occasionally, re-allocation is necessary and is performed, again allocating more memory than necessary Note: this is all done automatically by the string class

M M EECS498 EECS498 Andrew M Morgan10 Some Final string Functionality Several member functions are available to get information about a string –capacity: The number of characters that can be placed in a string without the inefficiency of re-allocating –length: The number of characters currently in the string You can manually change the capacity of a string –resize: Sets the capacity of a string to be at least a user-defined size –This can be useful if you know a string will be at most n characters long By resizing the string to capacity n only that amount of memory is associated with the string This prevents wasted memory when you know the exact size you need Additionally, it can help prevent numerous re-allocations if you will be appending on to the end of the string, but know the final size ahead of time

M M EECS498 EECS498 Andrew M Morgan11 string Example #4 int main(void) { string str; string str2; cout << "Str: " << str << endl; cout << "Length: " << str.length(); cout << " Cap: " << str.capacity(); cout << endl; str = "888"; cout << "Str: " << str << endl; cout << "Length: " << str.length(); cout << " Cap: " << str.capacity(); cout << endl; str += "-111-"; cout << "Str: " << str << endl; cout << "Length: " << str.length(); cout << " Cap: " << str.capacity(); cout << endl; str += "1723-9"; cout << "Str: " << str << endl; cout << "Length: " << str.length(); cout << " Cap: " << str.capacity(); cout << endl; str += "abcdefghijklmnopqrstuv"; cout << "Str: " << str << endl; cout << "Length: " << str.length(); cout << " Cap: " << str.capacity(); cout << endl; return (0); } Str: Length: 0 Cap: 0 Str: 888 Length: 3 Cap: 31 Str: Length: 8 Cap: 31 Str: Length: 14 Cap: 31 Str: abcdefghijklmnopqrstuv Length: 36 Cap: 63

M M EECS498 EECS498 Andrew M Morgan12 C-Strings: Arrays Of Characters Arrays of characters can be treated differently than arrays of other types Array of characters, terminated with a NULL: C-string Not all character arrays are strings –Must include the NULL character on end –Must ensure size of array allows room for NULL C-strings can be output directly using << –Other arrays can not! There are many predefined functions in header file string.h for modifying strings that will be discussed –Note: is for C-strings, is for the C++ standard string

M M EECS498 EECS498 Andrew M Morgan13 Simple C-string Program int main(void) { const int SIZE = 5; int i = 0; int iary[SIZE] = {2,4,6,8,10}; //NOT a C-string char cary[SIZE] = {'D','r','e','w','\0'}; //IS a C-string char cary2[SIZE] = "Drew"; //NULL automatic! – IS a C-string char cary3[SIZE] = {'H','e','l','l','o'}; //NOT a C-string cout << iary << endl; cout << cary << endl; cout << cary2 << endl; return (0); } 0xffbef8e0 Drew These are POTENTIAL results. First line is an address of the first element of the iary. Your specific results will vary (printing a different address)

M M EECS498 EECS498 Andrew M Morgan14 Common Problem int main(void) { const int SIZE = 4; char ary[SIZE]={'D','r','e','w'}; cout << ary << endl; return (0); } Drewÿ¾úL t ÿ 4 ÿ ÿ A ¾ ú L 0 R $ Initial Mem. t ÿ 4 ÿ ÿ A ¾ ú L 0 R $ Resulting Mem. D r e w Note: NULL character (0)

M M EECS498 EECS498 Andrew M Morgan15 Assigning C-strings While initialization and assignment seem the same, they are two different operations Assigning a C-string to a literal string (i.e. "Drew") is legal during initialization, using = at declaration-time Assigning a C-string to a literal string, using operator=, is NOT legal anywhere else in the program! C-string assignment can still be done, but you must call a function in string.h Function prototype: char * strcpy(char *dest, char *src);

M M EECS498 EECS498 Andrew M Morgan16 Using strcpy() const int SIZE=5; char cary[SIZE] = "Drew"; //Legal here! char cary2[SIZE]; //cary2 = "Drew"; //ACK! Don't do this! //strcpy() automatically appends a NULL //character to the end of the string. strcpy(cary2, "Drew"); //Ahh. Much better. cout << cary << endl; cout << cary2 << endl; Drew

M M EECS498 EECS498 Andrew M Morgan17 Comparing and Appending Two Strings Like assignment, comparison is not allowed with the "==" operator –Since these "strings" are really just character pointers, these operators would work on the pointer values (addresses), rather than the contents Must call a function from string.h. Prototype: –s1 and s2 are C-strings (char arrays, terminated with NULL) –Return integer is: 0 if strings are the same negative if s1 is "less than" s2 (not the same) positive is s1 is "greater than" s2 (not the same) Appending strings is done with a funtion. Prototype: –s1 and s2 are C-strings (char arrays, terminated with NULL) –If s1 was "Drew" and s2 was "Morgan" then after a call to strcat, s1 would contain "DrewMorgan" and s2 is unchanged int strcmp(char *s1, char *s2); char * strcat(char *s1, char *s2);

M M EECS498 EECS498 Andrew M Morgan18 Using strcmp() And strcat() const int SIZE=5; char cary[SIZE] = "Drew"; char cary2[SIZE] = "Blah"; //Don't forget the "==0" part!!! if (strcmp(cary, cary2) == 0) cout << "Same strings!" << endl; else cout << "Not the same!" << endl; //Don't forget the "==0" part!!! if (strcmp(cary, "Drew") == 0) cout << "Same strings!" << endl; else cout << "Not the same!" << endl; const int SIZE=15; char cary[SIZE] = "Drew"; //Need not fill array char cary2[SIZE] = "Morgan"; cout << "Before: " << cary << endl; strcat(cary, " "); strcat(cary, cary2); cout << "After: " << cary << endl; Not the same! Same strings! Before: Drew After: Drew Morgan

M M EECS498 EECS498 Andrew M Morgan19 Finding The Length of a String You often want to know how long a string is. Prototype: –The int being returned is the length of the string –It is not the length of the array –It is not the length of the string including the NULL Example: int strlen(char *s); cary[15] = "Drew"; cout << "Length: " << strlen(cary) << endl; Length: 4

M M EECS498 EECS498 Andrew M Morgan20 Arrays of C-Strings C-Strings are just arrays, use 2-D array for an array of strings char strAry[4][4] = {"and", "the", "one", "for"}; int i; strAry[0][1] = 'b'; strAry[0][2] = 'c'; strcpy(strAry[2], "two"); for (i = 0; i < 4; i++) { cout << strAry[i] << endl; } return (status); abc the two for

M M EECS498 EECS498 Andrew M Morgan21 Dynamically Alloc C-Strings It is easy to overestimate the length of a string –Use dynamic allocation instead const int MAXSIZE=2048; int len; char str[MAXSIZE]; //Only one big wasteful array char *newstr; //will dynamically declare a C-string //of just the right size cout << "Enter a string: "; cin.get(str, MAXSIZE, '\n'); //Get string from keyboard len = strlen(str); //Find the length of the string newstr = new char[len+1]; //Claim space, plus 1 for null strcpy(newstr, str); //Copy string to the compact ary cout << newstr << endl; delete [] newstr;