Operator Overloading Application: String Class
Learn about the C++ string library, which empowers developers with efficient string manipulation capabilities, including the application of operator overloading.
We'll cover the following
The <string>
library
The string
class in the C++ Standard Library provides a powerful tool for working with strings in C++. It offers a wide range of functionality for string manipulation and handling. This code snippet demonstrates some of the essential features and operations that can be performed using the string
class.
#include <iostream>#include <string>using namespace std;int main(){// String initializationstring str1 = "Hello"; // constructor "string(const char*)"" availablestring str2 = " World";// Concatenation using '+'string str3 = str1 + str2; // copy constructor is also availablecout << "Concatenation: " << str3 << endl; // cout << operator is overloaded// String length using 'length()' functioncout << "Length of str3: " << str3.length() << endl;// Accessing individual characters using 'char operator[](int)const' operator// which returns a readonly charactercout << "First character: " << str3[0] << endl;cout << "Last character: " << str3[str3.length() - 1] << endl;// Substring using 'substr()' functionstring substr = str3.substr(6, 5);cout << "Substring: " << substr << endl;// Comparison using '==' and '!=' operatorsstring str4 = "Hello World";if (str3 == str4)cout << "Strings are equal" << endl;elsecout << "Strings are not equal" << endl;// Relational operatorsstring str5 = "Apple";string str6 = "Banana";if (str5 < str6)cout << str5 << " is less than " << str6 << endl;elsecout << str5 << " is greater than or equal to " << str6 << endl;// Assignment using '=' operatorstring str7 = str3;cout << "Copied string: " << str7 << endl;// Clearing the string using 'clear()' functionstr7.clear();cout << "Cleared string: " << str7 << endl;// Appending strings using '+='string str8 = "Hello";str8 += " World";cout << "Appended string: " << str8 << endl;// Finding substrings using 'find()' functionstring str9 = "Hello World";size_t found = str9.find("World");if (found != string::npos) {cout << "Substring found at index: " << found << endl;} else {cout << "Substring not found" << endl;}// Replacing substrings using 'replace()' functionstring str10 = "Hello World";str10.replace(6, 5, "Universe");cout << "Replaced string: " << str10 << endl;// Erasing characters using 'erase()' functionstring str11 = "Hello World";str11.erase(5, 6);cout << "Erased string: " << str11 << endl;// Converting integer to string using 'to_string'int num = 42;string strNum = to_string(num);cout << "Converted integer to string: " << strNum << endl;// Using 'getline' to read a line of textstring input;cout << "Enter a line of text: ";getline(cin, input);cout << "You entered: " << input << endl;// Substring extraction using 'substr()' functionstring original = "Hello, World!";string substring = original.substr(7, 5);cout << "Substring: " << substring << endl;return 0;}
Enter the input below
This code demonstrates various operations and functionalities provided by the <string>
library in C++ STL. Here’s a breakdown of how the code works:
-
Lines 7–8 (String initialization): Two strings,
str1
andstr2
, are initialized with the valuesHello
andWorld
, respectively. -
Lines 11–12 (Concatenation): The strings
str1
andstr2
are concatenated using the+
operator, and the result is stored instr3
. -
Line 15 (String length): The
length()
function is used to determine the length ofstr3
. -
Lines 20–21 (Accessing individual characters): The individual characters of
str3
are accessed using the[]
operator. -
Line 24 (Substring extraction): The
substr()
function is used to extract a substring fromstr3
, starting at index6
with a length of5
. -
Comparison: In line 29-32:
str3
is compared withstr4
using the==
operator to check if they are equal and the output is displayed based on the result of that comparison. -
Lines 37–40 (Relational operators): The strings
str5
andstr6
are compared using the<
operator to determine if one string is less than the other, and the output is displayed based on the result of that comparison. -
Lines 43–44 (Assignment): The string
str3
is assigned tostr7
using the=
operator andstr7
is displayed using thecout
statement. -
Lines 47–48 (Clearing the string): The
clear()
function is called onstr7
to remove its contents. -
Lines 51–53 (Appending strings): The
+=
operator is used to append the stringWorld
tostr8
. -
Lines 56–62 (Finding substrings): The
find()
function is used to search for the substringWorld
withinstr9
. -
Lines 65–67 (Replacing substrings): The
replace()
function is used to replace a portion ofstr10
starting at index6
with the stringUniverse
. -
Lines 70–72 (Erasing characters): The
erase()
function is used to remove a portion ofstr11
starting at index5
with a length of6
. -
Line 75–77 (Converting an integer into a string): The
to_string()
function is used to convert an integer (num
) into a string (strNum
). -
Lines 80–83 (Using
getline()
to read a line of text): Thegetline()
function is used to read a line of text from the user and store it in the string variable input. -
Lines 87–88 (Substring extraction): The
substr()
function is used to extract a substring (substring
) from the original string (original
).