Download presentation
Presentation is loading. Please wait.
1
Problems With Character Arrays
Numerous problems exist when we use traditional character arrays in C++: Wouldn’t it be nice if we could assign a character array a new value? Wouldn’t it be nice to be able to concatenate two character arrays? Wouldn’t it be nice to be able to retrieve a particular substring? Wouldn’t it be nice to be able to insert and remove whole substrings? Well, all of this and much, much more is possible with the new string class, made possible with a simple #include <string> preprocessing statement! Savitch - Chapter 11 CS 140
2
Using The C++ Standard string Class
#include <iostream> #include <string> using namespace std; void main() { string firstName = "Fred"; string lastName = "Flintstone"; string fullName = firstName + ' ' + lastName; cout << fullName << endl << endl; fullName = "Wilma"; fullName += ' '; fullName += lastName; fullName.replace(0, 5, "Ms."); cout << fullName << endl; fullName.replace(4, 10, "Rubble"); fullName.insert(4, "Betty "); fullName[1] = 'r'; fullName.replace(5, 3, "arne"); return; } Notice it’s not cstring! Assignment statements, addition operators, output operators Additive assignment operators Member functions insert and replace Subscript operator Savitch - Chapter 11 CS 140
3
Substrings and Size Adjustment with strings
#include <iostream> #include <string> using namespace std; void main() { string name("Scooby Doo"); cout << name << endl << endl; string middle = name.substr(1,5); cout << middle << endl; middle.at(0) = name.at(7); cout << middle << endl; for (int i = 1; i <= 16; i++) { name.insert(7, middle + ' '); cout << name << endl; } cout << endl; return; at member function substr member function string dynamically adjusts its size to accommodate new insertions! Savitch - Chapter 11 CS 140
4
Lexicographical Ordering of strings Arrays of type string are legal!
#include <iostream> #include <string> using namespace std; void bubbleSort(string list[], int size); void main() { string quests[5] = {"jonny","Hadji","Race","bENTON","bandit"}; bubbleSort(quests, 5); for (int i = 0; i <= 4; i++) cout << quests[i] << endl; cout << endl; return; } Arrays of type string are legal! void bubbleSort(string list[], int size) { bool swapped = true; int lastIndex = size - 1; string tempString; while ((swapped) && (lastIndex > 0)) swapped = false; for (int i = 0; i < lastIndex; i++) if (list[i] > list[i+1]) tempString = list[i]; list[i] = list[i+1]; list[i+1] = tempString; swapped = true; } lastIndex--; <, >, <=, >=, ==, and != are all defined for class string, but they use a “lexicographical” ordering! Savitch - Chapter 11 CS 140
5
Comparing Parts of strings
#include <iostream> #include <string> using namespace std; int IbeforeEmistakes(string wd); int EbeforeImistakes(string wd); void main() { string word; int i; cout << "Enter a word (enter 'quit' to exit): "; cin >> word; while (word != "quit") for (i = 1; i <= IbeforeEmistakes(word); i++) cout << "I BEFORE E EXCEPT AFTER C!" << endl; for (i = 1; i <= EbeforeImistakes(word); i++) cout << "E BEFORE I ONLY AFTER C!" << endl; cout << endl; cout << "Enter another word (enter 'quit' to exit): "; } return; string parameters can be passed by value! string values can be input! Savitch - Chapter 11 CS 140
6
Comparing Parts of strings (Continued)
int IbeforeEmistakes(string wd) { int mistakeCount = 0; int index = wd.find("ie"); while (index >= 0) if ((index > 0) && (wd.find_first_of("Cc",index-1) == index-1)) mistakeCount++; index = wd.find("ie", index+2); } return mistakeCount; int EbeforeImistakes(string wd) int index = wd.find("ei"); if (index == 0) else if ((index > 0) && (wd.find_first_of("Cc",index-1) != index-1)) index = wd.find("ei", index+2); Finds the first index where the parameterized substring begins (returns -1 if it doesn’t occur)! Finds the first index where any character in the parameterized substring occurs, starting at the parameterized position (returns -1 if none of the characters occur from the parameterized position on)! Finds the first index where the parameterized substring begins starting at the parameterized position (returns -1 if it doesn’t occur from the parameterized position on)! Savitch - Chapter 11 CS 140
7
Comparing Parts of strings (Continued)
Savitch - Chapter 11 CS 140
8
Storage Allocation for a string length and size are identical
#include <iostream> #include <string> using namespace std; void main() { string firstWord = "Chapter One: The Beginning"; string secondWord = "Page 1"; cout << firstWord.size() << ' ' << firstWord.length() << ' ' << firstWord.max_size() << ' ' << firstWord.capacity() << endl; cout << firstWord << ' ' << secondWord << endl << endl; firstWord.resize(11); firstWord.resize(26); firstWord.resize(41,'*'); return; } length and size are identical max_size is the most the system can handle for one string; capacity is the current memory devoted to the string in question Savitch - Chapter 11 CS 140
9
Using strings to store filenames
#include <iostream> #include <fstream> #include <string> using namespace std; void main() { string filename = "horribleSciFi.txt"; ofstream outfile; outfile.open(filename.c_str()); outfile << "Wesley Crusher\nJar Jar Binks\nNeelix\nEwoks\n" << "Rick Berman & Brannan Braga\n\n"; outfile.close(); } The c_str() function translates the string into a cstring. Wesley Crusher Jar Jar Binks Neelix Ewoks Rick Berman & Brannan Braga Final contents of ‘horribleSciFi.txt’. Savitch - Chapter 11 CS 140
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.