Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

classString #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .idea/CPP.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions classString/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
33 changes: 33 additions & 0 deletions classString/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>
#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;

}
174 changes: 174 additions & 0 deletions classString/string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
#include <iostream>
#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;
}
31 changes: 31 additions & 0 deletions classString/string.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <iostream>

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);
};
1 change: 1 addition & 0 deletions cmake-build-debug/CMakeFiles/clion-Debug-log.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CMakeLists.txt not found in /Users/milana/CLionProjects/CPP Select CMakeLists.txt