Created
October 4, 2025 08:25
-
-
Save vlaleli/5f5412b1f5b36339d901b49dd39028cc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "MyString.hpp" | |
#include <iostream> | |
int main() { | |
MyString a("Hello"); | |
std::cout << a << std::endl; | |
MyString b; | |
std::cout << "Enter one word: "; | |
std::cin >> b; | |
std::cout << "You typed: " << b << std::endl; | |
MyString c("base"); | |
c = a; | |
std::cout << c << " (" << c.size() << ")\n"; | |
c = "new text"; | |
std::cout << c << " (" << c.size() << ")\n"; | |
c = nullptr; | |
std::cout << "[" << c << "] size=" << c.size() << "\n"; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "MyString.hpp" | |
#include <cstring> | |
int MyString::created_count = 0; | |
void MyString::ensure_capacity(int needLen) { | |
if (needLen + 1 <= capacity) return; | |
int newCap = capacity > 0 ? capacity : 1; | |
while (newCap < needLen + 1) newCap *= 2; | |
char* ns = new char[newCap]; | |
if (str) std::memcpy(ns, str, length + 1); | |
else ns[0] = '\0'; | |
delete[] str; | |
str = ns; | |
capacity = newCap; | |
} | |
MyString::MyString() : str(new char[80]), length(0), capacity(80) { | |
str[0] = '\0'; | |
++created_count; | |
} | |
MyString::MyString(int size) | |
: str(new char[size > 0 ? size : 1]), length(0), capacity(size > 0 ? size : 1) { | |
str[0] = '\0'; | |
++created_count; | |
} | |
MyString::MyString(const char* cstr) : str(nullptr), length(0), capacity(0) { | |
if (!cstr) { | |
str = new char[1]; str[0] = '\0'; | |
capacity = 1; length = 0; | |
} else { | |
length = static_cast<int>(std::strlen(cstr)); | |
capacity = length + 1; | |
str = new char[capacity]; | |
std::memcpy(str, cstr, capacity); | |
} | |
++created_count; | |
} | |
MyString::MyString(const MyString& other) | |
: str(new char[other.length + 1]), length(other.length), capacity(other.length + 1) { | |
std::memcpy(str, other.str, length + 1); | |
++created_count; | |
} | |
MyString::MyString(MyString&& other) noexcept | |
: str(other.str), length(other.length), capacity(other.capacity) { | |
other.str = nullptr; other.length = 0; other.capacity = 0; | |
++created_count; | |
} | |
MyString::~MyString() { delete[] str; } | |
void MyString::Read() { | |
std::string tmp; | |
std::getline(std::cin, tmp); | |
ensure_capacity(static_cast<int>(tmp.size())); | |
std::memcpy(str, tmp.c_str(), tmp.size()); | |
str[tmp.size()] = '\0'; | |
length = static_cast<int>(tmp.size()); | |
} | |
void MyString::Print() const { std::cout << (str ? str : "") << std::endl; } | |
void MyString::MyStrcpy(MyString& obj) { | |
ensure_capacity(obj.length); | |
std::memcpy(str, obj.str, obj.length + 1); | |
length = obj.length; | |
} | |
bool MyString::MyStrStr(const char* s) const { | |
if (!s) return false; | |
const char* pos = std::strstr(str, s); | |
return pos != nullptr; | |
} | |
int MyString::MyChr(char c) const { | |
if (!str) return -1; | |
const char* pos = std::strchr(str, static_cast<unsigned char>(c)); | |
return pos ? static_cast<int>(pos - str) : -1; | |
} | |
int MyString::MyStrLen() const { return length; } | |
void MyString::MyStrCat(MyString& b) { | |
ensure_capacity(length + b.length); | |
std::memcpy(str + length, b.str, b.length + 1); | |
length += b.length; | |
} | |
void MyString::MyDelChr(char c) { | |
if (!str) return; | |
int w = 0; | |
for (int r = 0; r < length; ++r) if (str[r] != c) str[w++] = str[r]; | |
str[w] = '\0'; | |
length = w; | |
} | |
int MyString::MyStrCmp(MyString& b) const { | |
int cmp = std::strcmp(str, b.str); | |
if (cmp < 0) return -1; | |
if (cmp > 0) return 1; | |
return 0; | |
} | |
MyString& MyString::operator=(const MyString& rhs) { | |
if (this == &rhs) return *this; | |
ensure_capacity(rhs.length); | |
std::memcpy(str, rhs.str, rhs.length + 1); | |
length = rhs.length; | |
return *this; | |
} | |
MyString& MyString::operator=(MyString&& rhs) noexcept { | |
if (this == &rhs) return *this; | |
delete[] str; | |
str = rhs.str; length = rhs.length; capacity = rhs.capacity; | |
rhs.str = nullptr; rhs.length = 0; rhs.capacity = 0; | |
return *this; | |
} | |
MyString& MyString::operator=(const char* cstr) { | |
if (!cstr) { | |
ensure_capacity(0); | |
str[0] = '\0'; length = 0; | |
return *this; | |
} | |
int n = static_cast<int>(std::strlen(cstr)); | |
ensure_capacity(n); | |
std::memcpy(str, cstr, n + 1); | |
length = n; | |
return *this; | |
} | |
MyString& MyString::operator+=(const MyString& rhs) { | |
ensure_capacity(length + rhs.length); | |
std::memcpy(str + length, rhs.str, rhs.length + 1); | |
length += rhs.length; | |
return *this; | |
} | |
char& MyString::operator[](int i) { | |
if (i < 0 || i >= length) throw std::out_of_range("index"); | |
return str[i]; | |
} | |
const char& MyString::operator[](int i) const { | |
if (i < 0 || i >= length) throw std::out_of_range("index"); | |
return str[i]; | |
} | |
void MyString::operator()() { Read(); } | |
MyString& MyString::operator++() { | |
ensure_capacity(length + 1); | |
std::memmove(str + 1, str, length + 1); | |
str[0] = '+'; ++length; | |
return *this; | |
} | |
MyString MyString::operator++(int) { | |
MyString old(*this); | |
ensure_capacity(length + 1); | |
str[length] = '+'; str[length + 1] = '\0'; ++length; | |
return old; | |
} | |
MyString operator+(MyString a, const MyString& b) { a += b; return a; } | |
MyString operator+(MyString a, char ch) { | |
a.ensure_capacity(a.length + 1); | |
a.str[a.length] = ch; a.str[a.length + 1] = '\0'; ++a.length; | |
return a; | |
} | |
MyString operator+(char ch, const MyString& b) { | |
MyString res(b); | |
res.ensure_capacity(b.length + 1); | |
std::memmove(res.str + 1, res.str, b.length + 1); | |
res.str[0] = ch; ++res.length; | |
return res; | |
} | |
MyString operator+(MyString a, int n) { | |
if (n < 0) n = 0; | |
a.ensure_capacity(a.length + n); | |
for (int i = 0; i < n; ++i) a.str[a.length + i] = '*'; | |
a.str[a.length + n] = '\0'; a.length += n; | |
return a; | |
} | |
MyString operator+(int n, const MyString& b) { | |
if (n < 0) n = 0; | |
MyString res(b); | |
res.ensure_capacity(b.length + n); | |
std::memmove(res.str + n, res.str, b.length + 1); | |
for (int i = 0; i < n; ++i) res.str[i] = '*'; | |
res.length += n; | |
return res; | |
} | |
bool operator==(const MyString& a, const MyString& b) { return std::strcmp(a.str, b.str) == 0; } | |
bool operator!=(const MyString& a, const MyString& b) { return !(a == b); } | |
const char* MyString::c_str() const { return str ? str : ""; } | |
int MyString::size() const { return length; } | |
std::ostream& operator<<(std::ostream& os, const MyString& s) { | |
return os << s.c_str(); | |
} | |
std::istream& operator>>(std::istream& is, MyString& s) { | |
std::string tmp; | |
is >> tmp; | |
s = tmp.c_str(); | |
return is; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <iostream> | |
#include <string> | |
#include <stdexcept> | |
class MyString { | |
char* str; | |
int length; | |
int capacity; | |
void ensure_capacity(int needLen); | |
public: | |
static int created_count; | |
MyString(); | |
explicit MyString(int size); | |
MyString(const char* cstr); | |
MyString(const MyString& other); | |
MyString(MyString&& other) noexcept; | |
~MyString(); | |
void Read(); | |
void Print() const; | |
void MyStrcpy(MyString& obj); | |
bool MyStrStr(const char* s) const; | |
int MyChr(char c) const; | |
int MyStrLen() const; | |
void MyStrCat(MyString& b); | |
void MyDelChr(char c); | |
int MyStrCmp(MyString& b) const; | |
MyString& operator=(const MyString& rhs); | |
MyString& operator=(MyString&& rhs) noexcept; | |
MyString& operator=(const char* cstr); | |
MyString& operator+=(const MyString& rhs); | |
char& operator[](int i); | |
const char& operator[](int i) const; | |
void operator()(); | |
MyString& operator++(); | |
MyString operator++(int); | |
const char* c_str() const; | |
int size() const; | |
friend MyString operator+(MyString a, const MyString& b); | |
friend MyString operator+(MyString a, char ch); | |
friend MyString operator+(char ch, const MyString& b); | |
friend MyString operator+(MyString a, int n); | |
friend MyString operator+(int n, const MyString& b); | |
friend bool operator==(const MyString& a, const MyString& b); | |
friend bool operator!=(const MyString& a, const MyString& b); | |
}; | |
std::ostream& operator<<(std::ostream& os, const MyString& s); | |
std::istream& operator>>(std::istream& is, MyString& s); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment