STRING

mail@pastecode.io avatar
unknown
c_cpp
16 days ago
1.6 kB
2
Indexable
Never
#include <iostream>
#include <string>
#include <algorithm>

class StringManipulator {
private:
    std::string str;

public:
    StringManipulator(const std::string& input) : str(input) {}

    void compare(const std::string& other) {
        if (str == other) {
            std::cout << "Strings are equal." << std::endl;
        } else {
            std::cout << "Strings are not equal." << std::endl;
        }
    }

    void replaceCase() {
        for (char& c : str) {
            if (std::isupper(c)) {
                c = std::tolower(c);
            } else if (std::islower(c)) {
                c = std::toupper(c);
            }
        }
        std::cout << "String after case replacement: " << str << std::endl;
    }

    void reverseString() {
        std::reverse(str.begin(), str.end());
        std::cout << "Reversed string: " << str << std::endl;
    }

    void upendString() {
        char temp;
        for (size_t i = 0; i < str.size() / 2; ++i) {
            temp = str[i];
            str[i] = str[str.size() - 1 - i];
            str[str.size() - 1 - i] = temp;
        }
        std::cout << "Upended string: " << str << std::endl;
    }

    void eraseString() {
        str.erase();
        std::cout << "String after erasing: " << str << std::endl;
    }
};

int main() {
    StringManipulator strManipulator("HelloWorld");
    strManipulator.compare("helloWorld");
    strManipulator.replaceCase();
    strManipulator.reverseString();
    strManipulator.upendString();
    strManipulator.eraseString();

    return 0;
}
Leave a Comment