0% found this document useful (0 votes)
4 views

Lab # 9 String

This document provides an overview of the C++ string class, highlighting its advantages over traditional C-strings, including automatic memory management and operator overloading. It covers various functionalities such as finding, modifying, and comparing string objects, along with examples of code demonstrating these operations. Additionally, it discusses other string functions related to size, capacity, and the underlying structure of the string class.

Uploaded by

samrasohail45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab # 9 String

This document provides an overview of the C++ string class, highlighting its advantages over traditional C-strings, including automatic memory management and operator overloading. It covers various functionalities such as finding, modifying, and comparing string objects, along with examples of code demonstrating these operations. Additionally, it discusses other string functions related to size, capacity, and the underlying structure of the string class.

Uploaded by

samrasohail45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Computer Programming

Lab Objectives
The objectives of this lab are to demonstrate.

1. String Class

2. Finding string objects

3. Modifying string objects

4. Comparing string objects

5. Other string functions

The Standard C++ string Class


Standard C++ includes a new class called string. This class improves on the traditional Cstring in
many ways. For one thing, you no longer need to worry about creating an array of the right size to
hold string variables. The string class assumes all the responsibility for memory management. Also,
the string class allows the use of overloaded operators, so you can concatenate string objects with the
+ operator:
s3 = s1 + s2
There are other benefits as well. This new class is more efficient and safer to use than C-strings were.
In most situations it is the preferred approach. (However, as we noted earlier, there are still many
situations in which C-strings must be used.) In this section we’ll examine the string class and its
various member functions and operators.

#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1(“Man”); //initialize
string s2 = “Beast”; //initialize
string s3;
s3 = s1; //assign
cout << “s3 = “ << s3 << endl;
s3 = “Neither “ + s1 + “ nor “; //concatenate
s3 += s2; //concatenate
cout << “s3 = “ << s3 << endl;
s1.swap(s2);
cout << s1 << “ nor “ << s2 << endl;
return 0;
}

s3 = Man
s3 = Neither Man nor Beast
Beast nor Man
Finding string Objects
The string class includes a variety of member functions for finding strings and substrings in
string objects.

//finding substrings in string objects


#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1 =
“In Xanadu did Kubla Kahn a stately pleasure dome decree”;
int n;
n = s1.find(“Kubla”);
cout << “Found Kubla at “ << n << endl;
n = s1.find_first_of(“spde”);
cout << “First of spde at “ << n << endl;
n = s1.find_first_not_of(“aeiouAEIOU”);
cout << “First consonant at “ << n << endl;
return 0;
}

Found Kubla at 14
First of spde at 7
First consonent at 1

Modifying string Objects


There are various ways to modify string objects. Our next example shows the member functions
erase(), replace(), and insert() at work.

#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1(“Quick! Send for Count Graystone.”);
string s2(“Lord”);
string s3(“Don’t “);
s1.erase(0, 7); //remove “Quick! “
s1.replace(9, 5, s2); //replace “Count” with “Lord”
s1.replace(0, 1, “s”); //replace ‘S’ with ‘s’
s1.insert(0, s3); //insert “Don’t “ at beginning
s1.erase(s1.size()-1, 1); //remove ‘.’
s1.append(3, ‘!’); //append “!!!”
int x = s1.find(‘ ‘); //find a space
while( x < s1.size() ) //loop while spaces remain
{
s1.replace(x, 1, “/”); //replace with slash
x = s1.find(‘ ‘); //find next space
}
cout << “s1: “ << s1 << endl;
return 0;
}
s1: Don’t/send/for/Lord/Graystone!!!

Comparing string Objects


You can use overloaded operators or the compare() function to compare string objects. These discover
whether strings are the same, or whether they precede or follow one another alphabetically.

#include <iostream>
#include <string>
using namespace std;
int main()
{
string aName = “George”;
string userName;
cout << “Enter your first name: “;
cin >> userName;
if(userName==aName) //operator ==
cout << “Greetings, George\n”;
else if(userName < aName) //operator <
cout << “You come before George\n”;
else
cout << “You come after George\n”;
//compare() function
int n = userName.compare(0, 2, aName, 0, 2);
cout << “The first two letters of your name “;
if(n==0)
cout << “match “;
else if(n < 0)
cout << “come before “;
else
cout << “come after “;
cout << aName.substr(0, 2) << endl;
return 0;
}

Enter your first name: Alfred


You come before George
The first two letters of your name come before Ge

Other string Functions


We’ve seen that size() and length() both return the number of characters currently in a string
object.The amount of memory occupied by a string is usually somewhat larger than that actually
needed for the characters. (Although if it hasn’t been initialized it uses 0 bytes for characters.) The
capacity() member function returns the actual memory occupied. You can add characters to the string
without causing it to expand its memory until this limit is reached.The max_size() member function
returns the maximum possible size of a string object. This amount corresponds to the size of int
variables on your system, less 3 bytes. In 32-bit Windows systems this is 4,294,967,293 bytes, but the
size of your memory will probably restrict this amount. Most of the string member functions we’ve
discussed have numerous variations in the numbers and types of arguments they take. Consult your
compiler’s documentation for details. You should be aware that string objects are not terminated with
a null or zero as C-strings are. Instead, the length of the string is a member of the class. So if you’re
stepping along the string, don’t rely on finding a null to tell you when you’ve reached the end.
The string class is actually only one of many possible string-like classes, all derived from the template
class basic_string. The string class is based on type char, but a common variant is to use type wchar_t
instead. This allows basic_string to be used for foreign languages with many more characters than
English. Your compiler’s help file may list the string member functions under basic_string.

You might also like