본문 바로가기

강의자료/C/C++

053. 스트링 클래스를 직접 만들어보자.


◈ 이 글은 강의를 위하여 제가 직접 작성한 내용입니다. 따라서 퍼가실 경우, 출처를 명확히 해주시기 바랍니다!!
◈ This ariticle was written by me for teaching. So if you want to copy this article, please clarify the source!!



#include 
using namespace std;

class MyString
{
private:
	char* buffer;

public:
	MyString() : buffer(NULL) {}
	MyString(const char* str) : buffer(NULL)
	{
		*this = str;
	}
	MyString(const MyString& mystr) : buffer(NULL)
	{
		*this = mystr;
	}
	~MyString()
	{
		if(buffer) delete [] buffer;
		cout << "destructor" << endl;
	}

	const char* c_str() const
	{
		return buffer;
	}

	MyString& operator = (const char* str)
	{
		if(buffer) delete [] buffer;

		int count = strlen(str)+1;
		buffer = new char[count];

		strcpy_s(buffer, count, str);

		return *this;
	}

	MyString& operator = (const MyString& mystr)
	{
		*this = mystr.c_str();
		return *this;
	}

	MyString substr(int start, int length) const
	{
		int size = length+1;
		MyString substring;
		substring.buffer = new char[size];
		strncpy_s(substring.buffer, size, buffer+start, length);
		substring.buffer[length] = '\0';

		return substring;
	}

	MyString& operator += (const char* str)
	{
		int count = strlen(buffer) + strlen(str) + 1;
		char* temp = new char[count];

		if(buffer) 
		{
			strcpy_s(temp, count, buffer);
			delete [] buffer;
		}

		buffer = temp;

		strcat_s(buffer, count, str);

		return *this;
	}

	MyString& operator += (const MyString& mystr)
	{
		*this += mystr.c_str();
		return *this;
	}

	bool operator == (const char* str)
	{
		return ( strcmp(buffer, str) == 0 );
	}
	
	bool operator == (const MyString& mystr)
	{
		return ( *this == mystr.c_str() );
	}

	void erase(int start, int length)
	{
		if(buffer == NULL) return;

		int count = strlen(buffer) - length + 1;
		char* temp = new char[count];

		strncpy_s(temp, count, buffer, start);
		strcat_s(temp, count, buffer+start+length);

		delete [] buffer;

		buffer = temp;
	}

	int size() const
	{
		return strlen(buffer);
	}

	int length() const
	{
		return strlen(buffer);
	}

	int find(const char c) const
	{
		int len = strlen(buffer);

		for(int i=0; i <= len; i++)
		{
			if(buffer[i] == c)
			{
				return i;
			}
		}

		return -1;
	}

	int find(const char* str) const
	{
		int len = strlen(buffer);
		int strlength = strlen(str);

		for(int i=0; i <= len-strlength; i++)
		{
			if(buffer[i] == str[0])
			{
				bool sameall = true;

				for(int n=1; n < strlength; n++)
				{
					if(buffer[i+n] != str[n])
					{
						sameall = false;
						break;
					}
				}

				if(sameall) return i;
			}
		}

		return -1;
	}

	int find(const MyString& mystr) const
	{
		return find(mystr.c_str());
	}
	
	void replace(int start, int length, char* str)
	{
		if(buffer == NULL) return;

		int count = strlen(buffer) - length + strlen(str) + 1;
		char* temp = new char[count];

		strncpy_s(temp, count, buffer, start);
		strcat_s(temp, count, str);
		strcat_s(temp, count, buffer+start+length);

		delete [] buffer;

		buffer = temp;
	}

	void getline()
	{
		char temp[1024];
		cin.getline(temp, _countof(temp));

		*this = temp;
	}

	bool empty() const
	{
		return (size() == 0);
	}

	char& operator [] (int idx)
	{
		return buffer[idx];
	}
	int operator [] (const MyString& mystr) const
	{
		return find(mystr);
	}
	int operator [] (const char* str) const
	{
		return find(str);
	}
	int operator [] (const char c) const
	{
		return find(c);
	}
};

ostream& operator << (ostream& os, const MyString& right)
{
	os << right.c_str();
	return os;
}
istream& operator >> (istream& is, MyString& right)
{
	right.getline();
	return is;
}

int main( )
{
	MyString src = "MBC 아카데미 디지털 교육원";
	MyString dest;

	cout << endl << ">> #include 없이 출력하기." << endl;
	cout << src.c_str() << endl;

	cout << endl << ">> 문자열 복사." << endl;
	dest = src;
	cout << dest << endl;

	cout << endl << ">> 문자열의 일부만 복사." << endl;
	dest = src.substr(0, 12);	// 여기서 2번의 소멸자가 호출된다.
	cout << dest << endl;

	MyString str1("MBC 아카데미 ");
	MyString str2 = "디지털 교육원";

	cout << endl << ">> 문자열 합성." << endl;
	str1 += str2;
	cout << str1 << endl;

	cout << endl << ">> 문자열의 일부만 합성." << endl;
	str1 += str2.substr(0, 6);
	cout << str1 << endl;

	cout << endl << ">> 문자열 비교." << endl;
	if(src == str1) cout << "같은 문자열." << endl;
	else cout << "다른 문자열." << endl;

	cout << endl << ">> 문자열 삭제." << endl;
	src.erase(3, 9);
	cout << src << endl;

	cout << endl << ">> 문자열 길이." << endl;
	cout << src.size() << " / " << src.length() << endl;

	cout << endl << ">> 문자열 검색 : 실패시 -1을 리턴." << endl;
	MyString key = "디지털";
	cout << "문자열 검색 : " << src.find(key) << endl;
	cout << "문자열 검색 : " << src[key] << endl;
	cout << "문자열 검색 : " << src["디지털"] << endl;

	cout << endl << ">> 문자열 변경." << endl;
	src.replace(0, 3, "Korean BoroadCast");
	cout << src << endl;

	cout << endl << ">> 문자열 입력 : getline()" << endl;
	//getline(cin, dest);
	dest.getline();
	if(dest.empty()) 
		cout << "아무것도 입력하지 않았습니다." << endl;
	else 
		cout << dest << endl;

	cout << endl << ">> 문자열 입력 : cin" << endl;
	cin >> dest;
	if(dest.empty()) 
		cout << "아무것도 입력하지 않았습니다." << endl;
	else 
		cout << dest << endl;

	cout << endl << ">> 문자열 앞글자." << endl;
	if(dest.empty() == false) cout << dest[0] << endl;

	cout << endl << "-- End --" << endl;
	return 0;
}