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.

Press + to interact
#include <iostream>
#include <string>
using namespace std;
int main()
{
// String initialization
string str1 = "Hello"; // constructor "string(const char*)"" available
string str2 = " World";
// Concatenation using '+'
string str3 = str1 + str2; // copy constructor is also available
cout << "Concatenation: " << str3 << endl; // cout << operator is overloaded
// String length using 'length()' function
cout << "Length of str3: " << str3.length() << endl;
// Accessing individual characters using 'char operator[](int)const' operator
// which returns a readonly character
cout << "First character: " << str3[0] << endl;
cout << "Last character: " << str3[str3.length() - 1] << endl;
// Substring using 'substr()' function
string substr = str3.substr(6, 5);
cout << "Substring: " << substr << endl;
// Comparison using '==' and '!=' operators
string str4 = "Hello World";
if (str3 == str4)
cout << "Strings are equal" << endl;
else
cout << "Strings are not equal" << endl;
// Relational operators
string str5 = "Apple";
string str6 = "Banana";
if (str5 < str6)
cout << str5 << " is less than " << str6 << endl;
else
cout << str5 << " is greater than or equal to " << str6 << endl;
// Assignment using '=' operator
string str7 = str3;
cout << "Copied string: " << str7 << endl;
// Clearing the string using 'clear()' function
str7.clear();
cout << "Cleared string: " << str7 << endl;
// Appending strings using '+='
string str8 = "Hello";
str8 += " World";
cout << "Appended string: " << str8 << endl;
// Finding substrings using 'find()' function
string 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()' function
string str10 = "Hello World";
str10.replace(6, 5, "Universe");
cout << "Replaced string: " << str10 << endl;
// Erasing characters using 'erase()' function
string 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 text
string input;
cout << "Enter a line of text: ";
getline(cin, input);
cout << "You entered: " << input << endl;
// Substring extraction using 'substr()' function
string 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 and str2, are initialized with the values Hello and World, respectively.

  • Lines 11–12 (Concatenation): The strings str1 and str2 are concatenated using the + operator, and the result is stored in str3.

  • Line 15 (String length): The length() function is used to determine the length of str3.

  • 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 from str3, starting at index 6 with a length of 5.

  • Comparison: In line 29-32: str3 is compared with str4 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 and str6 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 to str7 using the = operator and str7 is displayed using the cout statement.

  • Lines 47–48 (Clearing the string): The clear() function is called on str7 to remove its contents.

  • Lines 51–53 (Appending strings): The += operator is used to append the string World to str8.

  • Lines 56–62 (Finding substrings): The find() function is used to search for the substring World within str9.

  • Lines 65–67 (Replacing substrings): The replace() function is used to replace a portion of str10 starting at index 6 with the string Universe.

  • Lines 70–72 (Erasing characters): The erase() function is used to remove a portion of str11 starting at index 5 with a length of 6.

  • 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): The getline() 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).