SavitchChap11 - Problems With Character Arrays Numerous...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Savitch - Chapter 11 CS 150 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!
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Savitch - Chapter 11 CS 150 2 Using The C++ Standard string Class #include <iostream> #include <string> using namespace std; // This indicates that we're // using the new ANSI Standard C++ void main() { string firstName = "Fred"; string lastName = "Flintstone"; string fullName = firstName + ' ' + lastName; cout << fullName << endl << endl; fullName = "Wilma"; fullName += ' '; fullName += lastName; cout << fullName << endl << endl; fullName.replace(0, 5, "Ms."); cout << fullName << endl; fullName.replace(4, 10, "Rubble"); cout << fullName << endl; fullName.insert(4, "Betty "); cout << fullName << endl << endl; fullName[1] = 'r'; fullName.replace(5, 3, "arne"); cout << fullName << endl << endl; return; } Notice the lack of “.h” extensions! Notice the use of
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

SavitchChap11 - Problems With Character Arrays Numerous...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online