#include <iostream>
#include <string>
#include <algorithm>

int main() {
    // Creating and initializing strings
    std::string str1 = "Hello, World!";
    std::string str2 = "Goodbye!";
    char char_array[20];

    // size() and length()
    std::cout << "Size of str1: " << str1.size() << std::endl;
    std::cout << "Length of str1: " << str1.length() << std::endl;

    // push_back() and pop_back()
    str1.push_back('!');
    std::cout << "After push_back: " << str1 << std::endl;
    str1.pop_back();
    std::cout << "After pop_back: " << str1 << std::endl;

    // resize()
    str1.resize(5);
    std::cout << "After resize: " << str1 << std::endl;

    // swap()
    str1.swap(str2);
    std::cout << "After swap, str1: " << str1 << ", str2: " << str2 << std::endl;

    // find()
    size_t pos = str1.find("bye");
    if (pos != std::string::npos)
        std::cout << "\"bye\" found at position: " << pos << std::endl;
    else
        std::cout << "\"bye\" not found" << std::endl;

    // erase()
    str2.erase(0, 4);
    std::cout << "After erase: " << str2 << std::endl;

    // compare()
    int result = str1.compare(str2);
    if (result == 0)
        std::cout << "str1 is equal to str2" << std::endl;
    else if (result > 0)
        std::cout << "str1 is greater than str2" << std::endl;
    else
        std::cout << "str1 is less than str2" << std::endl;

    // substr()
    std::string sub = str1.substr(0, 4);
    std::cout << "Substring of str1: " << sub << std::endl;

    // at()
    std::cout << "Character at position 2: " << str1.at(2) << std::endl;

    // copy()
    str1.copy(char_array, 5, 0);
    char_array[5] = '\0'; // Null-terminate the copied char array
    std::cout << "Copied characters to char_array: " << char_array << std::endl;

    // capacity()
    std::cout << "Capacity of str1: " << str1.capacity() << std::endl;

    // replace()
    str1.replace(0, 5, "Hi");
    std::cout << "After replace: " << str1 << std::endl;

    // empty()
    if (str1.empty())
        std::cout << "str1 is empty" << std::endl;
    else
        std::cout << "str1 is not empty" << std::endl;

    // append()
    str1.append(" there!");
    std::cout << "After append: " << str1 << std::endl;

    // shrink_to_fit()
    str1.shrink_to_fit();
    std::cout << "Capacity after shrink_to_fit: " << str1.capacity() << std::endl;

    // data() and c_str()
    const char* cstr1 = str1.data();
    const char* cstr2 = str1.c_str();
    std::cout << "Data: " << cstr1 << std::endl;
    std::cout << "C-String: " << cstr2 << std::endl;

    // begin() and end()
    std::cout << "Characters in str1 using iterators: ";
    for (auto it = str1.begin(); it != str1.end(); ++it) {
        std::cout << *it;
    }
    std::cout << std::endl;

    // rbegin() and rend()
    std::cout << "Characters in str1 using reverse iterators: ";
    for (auto rit = str1.rbegin(); rit != str1.rend(); ++rit) {
        std::cout << *rit;
    }
    std::cout << std::endl;

    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: