diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/CPP.iml b/.idea/CPP.iml new file mode 100644 index 0000000..f08604b --- /dev/null +++ b/.idea/CPP.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..5a853d8 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..095bf1d --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/classString/CMakeLists.txt b/classString/CMakeLists.txt new file mode 100644 index 0000000..c011521 --- /dev/null +++ b/classString/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.23) +project(CPP) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(CPP + string.cpp string.h main.cpp) \ No newline at end of file diff --git a/classString/main.cpp b/classString/main.cpp new file mode 100644 index 0000000..6afc01b --- /dev/null +++ b/classString/main.cpp @@ -0,0 +1,33 @@ +#include +#include "string.h" + +using namespace std; + +int main(){ + String firstString("Milana"); + String secondString("Zinovieva"); + String emptyString; + String cloneString(firstString); + String result = firstString + secondString; + cout << result; + cout << result.front() << endl; + cout << result.back() << endl; + cout << result.size() << endl; + bool check = result.empty(); + bool check1 = emptyString.empty(); + cout << check << check1 << endl; + firstString.clear(); + cout << firstString.size() << endl; + secondString.push_back('r'); + cout << secondString; + secondString.pop_back(); + cout << secondString; + secondString.insert("jj", 3); + cout << secondString; + String checkString("milashkakakashka"); + int m = checkString.find_first_of("sa"); + cout << m << endl; + String haha = checkString.substr(4, 8); + cout << haha << endl; + +} \ No newline at end of file diff --git a/classString/string.cpp b/classString/string.cpp new file mode 100644 index 0000000..1a11505 --- /dev/null +++ b/classString/string.cpp @@ -0,0 +1,174 @@ +#include +#include "string.h" + +using namespace std; + +String::String(){ + lengthString = 0; + dataString = new char[1]; + dataString[0] = '\0'; +} + +String::String(const char *string) { + lengthString = 0; + while(string[lengthString] != '\0'){ + lengthString++; + } + dataString = new char[lengthString + 1]; + for(int i = 0;i < lengthString;i++) + dataString[i] = string[i]; + dataString[lengthString + 1] = '\0'; +} + +String::String(const String &otherString) { + lengthString = otherString.lengthString; + dataString = new char[lengthString + 1]; + for (int i = 0;i < lengthString;i++) + dataString[i] = otherString.dataString[i]; + dataString[lengthString + 1] = '\0'; +} + +String::~String() { + delete[] dataString; +} + +const char* String::c_str() const { + return dataString; +} + +ostream& operator<< (ostream &out, const String &string) +{ + out << string.c_str() << endl; + return out; +} + +String String::operator+(const String& secondString) { + String result; + result.lengthString = secondString.lengthString + lengthString; + result.dataString = new char[result.lengthString + 1]; + for (int i = 0;i < lengthString;i++) + result.dataString[i] = dataString[i]; + for (int u = lengthString, t = 0;u < result.lengthString;u++, t++) { + result.dataString[u] = secondString.dataString[t]; + } + result.dataString[result.lengthString + 1] = '\0'; + return result; +} + +bool String::operator==(const String& other) { + int count = 0; + if (other.lengthString != lengthString){ + return false; + }else{ + for(int i = 0;i < lengthString;i++){ + if (other.dataString[i] == dataString[i]) + count++; + } + } + if (count == lengthString) + return true; + else + return false; +} + +char String::operator[](int number) { + return dataString[number]; +} + +char String::front() { //ready + if (lengthString == 0) + return ' '; + else + return dataString[0]; +} + +char String::back() { //ready + if (lengthString == 0) + return ' '; + else + return dataString[lengthString - 1]; +} + +int String::size() const { //ready + return lengthString; +} + +void String::clear() { //ready + for(int i = 0;i < lengthString;i++) + dataString[i] = '\0'; + lengthString = 0; +} + +bool String::empty() const { //ready + if (lengthString == 0) + return true; + else + return false; +} + +String String::insert(String need, int position) { //ready + String tempString; + for(int i = position, j = 0;i < lengthString;i++, j++) + tempString.dataString[j] = dataString[i]; + lengthString += need.lengthString; + for(int x = position, y = 0;x < position + need.lengthString;x++, y++) + dataString[x] = need.dataString[y]; + for(int h = position + need.lengthString, n = 0;h < lengthString;h++, n++) + dataString[h] = tempString.dataString[n]; + return dataString; +} + +String String::push_back(char symbol) { //ready + lengthString++; + dataString[lengthString - 1] = symbol; + return dataString; +} + +String String::pop_back() { //ready + dataString[lengthString - 1] = '\0'; + lengthString--; + return dataString; +} + +int String::find_first_of(String subString) { //ready + int i = 0; + while(i < lengthString){ + int count = 0; + int j = 0; + while (j < subString.lengthString){ + if (dataString[i + j] == subString[j]) + count++; + j++; + } + if (count == subString.lengthString) + return i; + i++; + } + return -1; +} + +String String::substr(int startPosition, int endPosition) { // ready + String newString; + for(int i = startPosition, j = 0;i < endPosition;i++, j++) + newString.dataString[j] = dataString[i]; + return newString; +} + +String String::replace(int position, int count, const String& newString) { + for(int i = position;i < position + count;i++) + dataString[i] = '\0'; + lengthString = lengthString - count + newString.lengthString; + for(int j = position;j < lengthString;j++){ + dataString[j] = newString.dataString[j]; + } + return dataString; +} + +String String::append(const String& secondString) { + int tempLength = lengthString; + lengthString = lengthString + secondString.lengthString; + for (int i = tempLength, j = 0;i < lengthString;i++, j++){ + dataString[i] = secondString.dataString[j]; + } + return dataString; +} diff --git a/classString/string.h b/classString/string.h new file mode 100644 index 0000000..d02c033 --- /dev/null +++ b/classString/string.h @@ -0,0 +1,31 @@ +#include + +using namespace std; + +class String{ +private: + char* dataString; + int lengthString; + const char* c_str() const; +public: + String(); + String(const char* string); + String(const String& otherString); + ~String(); + friend ostream& operator<< (ostream &out, const String &string); + String operator +(const String& secondString); + bool operator ==(const String& other); + char operator [](int number); + char front(); + char back(); + int size() const; + void clear(); + bool empty() const; + String insert(String need, int position); + String push_back(char symbol); + String pop_back(); + String append(const String& secondString); + String replace(int position, int count, const String& newString); + String substr(int startPosition, int endPosition); + int find_first_of(String subString); +}; \ No newline at end of file diff --git a/cmake-build-debug/CMakeFiles/clion-Debug-log.txt b/cmake-build-debug/CMakeFiles/clion-Debug-log.txt new file mode 100644 index 0000000..726dd27 --- /dev/null +++ b/cmake-build-debug/CMakeFiles/clion-Debug-log.txt @@ -0,0 +1 @@ +CMakeLists.txt not found in /Users/milana/CLionProjects/CPP Select CMakeLists.txt