EECS402 Lecture 08. Intro To The Standard string Class. Some string Functionality

Similar documents
Engineering Problem Solving with C++, Etter

Strings and Stream I/O

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

Chapter 10 Characters, Strings, and the string class

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

Computer Science & Engineering 150A Problem Solving Using Computers

BITG 1113: Array (Part 2) LECTURE 9

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

Chapter 10: Characters, C- Strings, and More About the string Class

Chapter 10: Character Testing. From Program Character Case Conversion 8/23/2014. Character Testing. Character Case Conversion

String Class in C++ When the above code is compiled and executed, it produces result something as follows: cin and strings

Chapter 10: Characters, C- Strings, and More About the string Class Character Testing

Strings in C++ Dr. Ferdin Joe John Joseph Kamnoetvidya Science Academy

LECTURE 15. String I/O and cstring library

8. Characters, Strings and Files


Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

CS106L Handout #05 Fall 2007 October 3, C Strings

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

C++ Lab 02 - Command Line Arguments and Strings

C Style Strings. Lecture 11 COP 3014 Spring March 19, 2018

The University Of Michigan. EECS402 Lecture 05. Andrew M. Morgan. Savitch Ch. 5 Arrays Multi-Dimensional Arrays. Consider This Program

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

C-String Library Functions

Object Oriented Programming COP3330 / CGS5409

Lecture 3 The character, string data Types Files

Principles of Programming Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns

C++ Programming: From Problem Analysis to Program Design, Third Edition

CS 33. Introduction to C. Part 5. CS33 Intro to Computer Systems V 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Array Initialization

Integer Data Types. Data Type. Data Types. int, short int, long int

CS 31 Discussion 1A, Week 8. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

13 4 Understanding Character Strings

Strings. Arrays of characters. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

CSCE 110 PROGRAMMING FUNDAMENTALS

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14

Pointers, Arrays, and Strings. CS449 Spring 2016

EECS402 Lecture 02. Functions. Function Prototype

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

In this chapter, you will learn about: An Array Type for Strings. The Standard string Class. Vectors. Introduction Computer Science 1 CS 23021

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

Part 3: Function Pointers, Linked Lists and nd Arrays

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

Exercise 1.1 Hello world

Introduction to string

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

CS 103 Lab 6 - Party Like A Char Star

CS106X Handout 04 Autumn 2009 September 25 th, 2009 C++ Strings

C strings. (Reek, Ch. 9) 1 CS 3090: Safety Critical Programming in C

CSC 2400: Computer Systems. Arrays and Strings in C

Using Character Arrays. What is a String? Using Character Arrays. Using Strings Life is simpler with strings. #include <stdio.

For example, let s say we define an array of char of size six:

The University Of Michigan. EECS402 Lecture 02. Andrew M. Morgan. Savitch Ch. 3-4 Functions Value and Reference Parameters.

CS 103 Lab - Party Like A Char Star

MORE STRINGS AND RECURSION

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

CS 115 Exam 3, Spring 2010

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

CSC 2400: Computer Systems. Arrays and Strings in C

Chapter 8: Character & String. In this chapter, you ll learn about;

Computers Programming Course 11. Iulian Năstac

Pointers, Dynamic Data, and Reference Types

Lecture 12. We have already used strings. Strings. Hello Class is a string.

Strings in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Ch 6-8. C-Strings. April 30, Prof. Young-Tak Kim

System Design and Programming II

CSE 230 Intermediate Programming in C and C++ Arrays, Pointers and Strings

Copyright 2003 Pearson Education, Inc. Slide 1

Lecture 10. Command line arguments Character handling library void* String manipulation (copying, searching, etc.)

COMP1917: 09 Arrays and Strings

Characters, Character Strings, and string-manipulation functions in C

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

Objectives. In this chapter, you will:

EECS402 Lecture 24. Something New (Sort Of) Intro To Function Pointers

Personal SE. Functions, Arrays, Strings & Command Line Arguments

Lecture 10 Arrays (2) and Strings. UniMAP SEM II - 11/12 DKT121 1

C++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters

CSc Introduction to Computing

EE 355 Lab 4 - Party Like A Char Star

ONE DIMENSIONAL ARRAYS

LECTURE 02 INTRODUCTION TO C++

Iosif Ignat, Marius Joldoș Laboratory Guide 9. Character strings CHARACTER STRINGS

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

CSI33 Data Structures

Procedural Programming & Fundamentals of Programming

Managing Memory. (and low level Data Structures) Lectures 22, 23. Hartmut Kaiser.

EL2310 Scientific Programming

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

Exercise: Inventing Language

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

Computer Programming: Skills & Concepts (CP) Strings

Programming in C. Session 7. Seema Sirpal Delhi University Computer Centre

In the case of the dynamic array, the space must be deallocated when the program is finished with it. free(x);

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

Transcription:

The University Of Michigan Lecture 08 Andrew M. Morgan Savitch Ch. 9.0 C++ String Data Type C-Strings 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 <string> using the standard namespace to get C++ standard string functionality Note: This is different from #include'ing <string.h> 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 Andrew M Morgan 2 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 Andrew M Morgan 3 1

#include <iostream> #include <string> using namespace std; string first; string last("morgan"); string Example #1 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 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; Andrew M Morgan 4 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 Andrew M Morgan 5 string Example #2 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"; 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"; 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"; 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"; oper: s3 less than s1 comp: s3 less than s1 oper: s3 is equal to s1 comp: s3 is equal to s1 Andrew M Morgan 6 2

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); search at the index "startfrom", or "string::npos" if none of the chars are found Andrew M Morgan 7 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 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; string Example #3 Substr: how Comma: 5 how: 7 Spaces: 6 10 14 Punct and spaces: 5 6 10 14 18 Andrew M Morgan 8 cout << "Spaces:"; spaceloc = mystr.find(" "); while (spaceloc!= string::npos) cout << " " << spaceloc; spaceloc = mystr.find(" ", spaceloc + 1); cout << "Punct and spaces:"; loc = mystr.find_first_of(",?", 0); while (loc!= string::npos) cout << " " << loc; loc = mystr.find_first_of(",?", loc + 1); 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 reallocation 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 Andrew M Morgan 9 3

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 Andrew M Morgan 10 string Example #4 str += "-111-"; cout << "Str: " << str << endl; string str; string str2; cout << " Cap: " << str.capacity(); cout << "Str: " << str << endl; str += "1723-9"; cout << " Cap: " << str.capacity(); cout << "Str: " << str << endl; cout << " Cap: " << str.capacity(); str = "888"; cout << "Str: " << str << endl; str += "abcdefghijklmnopqrstuv"; cout << " Cap: " << str.capacity(); cout << "Str: " << str << endl; cout << " Cap: " << str.capacity(); Str: Length: 0 Cap: 0 Str: 888 Length: 3 Cap: 31 Str: 888-111- Length: 8 Cap: 31 Str: 888-111-1723-9 Length: 14 Cap: 31 Str: 888-111-1723-9abcdefghijklmnopqrstuv Length: 36 Cap: 63 Andrew M Morgan 11 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: <string.h> is for C-strings, <string> is for the C++ standard string Andrew M Morgan 12 4

Simple C-string Program 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; 0xffbef8e0 Drew 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) Andrew M Morgan 13 Common Problem Initial Mem. 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 100A t 4 ¾ ú L 0 R $ const int SIZE = 4; char ary[size]='d','r','e','w'; cout << ary << endl; Drew¾úL Resulting Mem. 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 100A t 4 ¾ ú L 0 R $ D r e w Note: NULL character (0) Andrew M Morgan 14 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); Andrew M Morgan 15 5

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 Drew Andrew M Morgan 16 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: Andrew M Morgan 17 int strcmp(char *s1, char *s2); 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: char * strcat(char *s1, char *s2); 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 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; Using strcmp() And strcat() Not the same! Same strings! //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); Before: Drew After: Drew Morgan cout << "After: " << cary << endl; Andrew M Morgan 18 6

Finding The Length of a String You often want to know how long a string is. Prototype: int strlen(char *s); 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: cary[15] = "Drew"; cout << "Length: " << strlen(cary) << endl; Length: 4 Andrew M Morgan 19 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 Andrew M Morgan 20 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; Andrew M Morgan 21 7

Command Line Parameters Until now, main has taken no parameters Often you want to provide inputs to main, without requiring user interaction Main can optionally take two parameters int argc: A count of the number of items typed on the command line Name of executable counts too! char *argv[]: An array of C-strings, one per item on the command line Name of executable is always argv[0] Each item is a C-string Even if you type "147' it will be treated as the C-string "147", NOT the integer 147 Andrew M Morgan 22 Command Line Parameters - Example int main(int argc, char *argv[]) int i; for (i = 0; i < argc; i++) cout << "Item #" << i << ": " << argv[i] << endl; cmdprompt%./cmdlinedemo Item #0:./cmdLineDemo cmdprompt%./cmdlinedemo hello 147 88.3 Item #0:./cmdLineDemo Item #1: hello Item #2: 147 Item #3: 88.3 Andrew M Morgan 23 Usage Statements In Programs If your program requires input parameters, it should include a usage statement How the user is notified about the correct way to run your program int main(int argc, char *argv[]) ifstream infile; if (argc!= 3) cout << "Usage: " << argv[0] << " <inputfilename> <value>" << endl; exit(0); infile.open(argv[1]); cout << "Value is: " << argv[2] << endl; //... more code to read //... input file, etc... cmdprompt% usagedemo Usage: usagedemo <inputfilename> <value> cmdprompt% usagedemo infile.txt Usage: usagedemo <inputfilename> <value> cmdprompt% usagedemo infile.txt 45 extrainfo Usage: usagedemo <inputfilename> <value> cmdprompt% usagedemo infile.txt 45 Value is: 45 Andrew M Morgan 24 8

Simple Conversion of C-Strings Andrew M Morgan 25 There are some function that allow easy conversion of C-strings to numeric types int atoi(char *cstring) Converts the ASCII C-string "cstring" to its integer representation. atoi is read "ASCII to Integer" If conversion isn't possible (invalid format), return value is undefined (usually 0) No good way to perform error checking, since any integer that it returns would be a valid integer double atof(char *cstring) Converts the ASCII C-string "cstring" to its double precision floating point representation. atof is read "ASCII to Float" (actually returns a double) If conversion isn't possible (invalid format), return value is undefined (usually 0) No good way to perform error checking, since any integer that it returns would be a valid double Simple Conversion of C-Strings, Example int main(int argc, char *argv[]) int intval; double doubleval; if (argc!= 3) cout << "Usage: " << argv[0] << " <floatval> <intval>" << endl; exit(2); doubleval = atof(argv[1]); intval = atoi(argv[2]); cout << doubleval << " / " << intval << = " << (doubleval / intval) << endl; cmdprompt% cstrconvdemo Usage:./cstrConvDemo <floatval> <intval> cmdprompt% cstrconvdemo 92.75 16 92.75 / 16 = 5.79688 cmdprompt% cstrconvdemo hello 18 0 / 18 = 0 cmdprompt% cstrconvdemo 92.75 16.75 92.75 / 16 = 5.79688 Bad # params Good run Non-parseable values typically result in 0 atoi converts only as much as it can to an int Andrew M Morgan 26 9