Strings Manipulation

Manipulate strings using '<string>' STL in C++.

<string>

In C++, the Standard Template Library (STL) provides a variety of powerful tools for working with strings. The primary class for string manipulation in C++ is 'std::string', which is part of the '<string>' header. Here is simple the way to create a string using STL:

cpp Copy Code
#include<iostream>
#include<string>

int main() {
    // Creating a string
    std::string myString = "Hello, Friend!";
    std::cout << "String: " << myString << std::endl;
    return 0;
}
Output:
String: Hello, Friend!

Common operations using the '<string>' STL in C++:

1. Concatenation:

std::string str3 = str1 + str2;  // Concatenates two strings
str1 += "Hello";                  // Appends a string to another

2. Accessing Characters:

char ch = str1[0];                 // Accesses the character at index 0
char firstChar = str1.front();      // Gets the first character
char lastChar = str1.back();        // Gets the last character

3. String Length:

int length = str1.length();        // Gets the length of the string

4. Comparisons:

if (str1 == str2)                  // Checks if two strings are equal
if (str1.compare(str2) == 0)       // Compares two strings lexicographically

5. Finding Substrings:

std::string substr = str1.substr(1, 3);

Extracts a substring starting at index 1 and of length 3.

6. Insertion and Erasure:

str1.insert(2, "XYZ");             // Inserts "XYZ" at index 2
str1.erase(1, 3);                   // Erases 3 characters starting at index 1

7. Conversion to C-style Strings:

const char* cstr = str1.c_str();

Gets a pointer to the C-style string representation.

8. Empty Check:

if (str1.empty()) // Checks if the string is empty

9. Iterating Through Characters:

for (char c : str1) {
    // Iterate through each character in the string
}

10. Resizing:

str1.resize(10);

Resizes the string to have a length of 10.

* These are just a few examples of the many operations you can perform using the '<string>' STL in C++. The header provides a comprehensive set of functions for efficient string manipulation.

Let's create a program to demonstrate all the avobe STL operations mentioned:

cpp Copy Code
#include<iostream>
#include<string>

 int main() {
 // Declaration and Initialization
 std::string str1 = "Hello, ";
 std::string str2 = "Friend!";
 
 // Concatenation
 std::string str3 = str1 + str2;
 
 // Accessing Characters
 char ch = str1[0];
 char firstChar = str1.front();
 char lastChar = str1.back();
 
 // String Length
 int length = str1.length();
 
 // Comparisons
 bool isEqual = (str1 == str2);
 bool isLexicEql = (str1.compare(str2) == 0);
 
 // Substring
 std::string substr = str1.substr(1, 3);
 
 // Finding Substrings
 size_t pos = str1.find("lo");
 
 // Insertion and Erasure
 str1.insert(6, "Beautiful ");
 str1.erase(3, 2);
 
 // Conversion to C-style Strings
 const char* cstr = str1.c_str();
 
 // Empty Check
 bool isEmpty = str1.empty();
 
 // Iterating Through Characters
 for (char c : str1) {
 std::cout << c << " ";
 }
 
 std::cout << "\n";
 
 // Resizing
 str1.resize(10);
 
 // Output Results
 std::cout << "Concatenated String: " << str3 << "\n";
 std::cout << "Accessed Character: " << ch << "\n";
 std::cout << "First Character: " << firstChar << "\n";
 std::cout << "Last Character: " << lastChar << "\n";
 std::cout << "String Length: " << length << "\n";
 std::cout << "Are Strings Equal? " << std::boolalpha << isEqual << "\n";
 std::cout << "Strings Lexicographically Equal? " << isLexicEql << "\n";
 std::cout << "Substring: " << substr << "\n";
 std::cout << "Substring Position: " << pos << "\n";
 std::cout << "Inserted and Erased String: " << str1 << "\n";
 std::cout << "C-style String: " << cstr << "\n";
 std::cout << "Is String Empty? " << isEmpty << "\n";
 
 return 0;
 }
Output:
H e l , B e a u t i f u l     
Concatenated String: Hello, Friend!       
Accessed Character: H
First Character: H
Last Character:  
String Length: 7
Are Strings Equal? false
Strings Lexicographically Equal? false
Substring: ell
Substring Position: 3
Inserted and Erased String: Hel,Beauti
C-style String: Hel,Beauti
Is String Empty? false

* This program covers the mentioned operations and prints the results for each operation. Feel free to modify it or expand upon it based on your specific needs.

What's Next?

We actively create content for our YouTube channel and consistently upload or share knowledge on the web platform.