Answer to Question #315177 in C++ for khan

Question #315177

Implement your own string class using only primitive data types. Your string class should contain some of the same functionality as the string class in C++.

Your class should have the following member variables:

1. char *data

2. int length

Your object should have the following constructors and destructor:

1. String(): default constructor

2. String(int size)

3. String(char* str)

Note: You can assume that any character array that is sent as str will be terminated

by a null character (‘\0’)

4. String(const String &str)

5. ~String()

Your class should have the following functions:

1. int strLength()

2. void clear()

3. bool empty()

4. int charAt(char c)

5. char* getdata()

6. bool equals(char*str)

7. bool equalsIgnoreCase(char* str)

8. char* substring(char* substr, int startIndex)

9. char* substring(char* substr, int startIndex, int endIndex)

10. void print()


1
Expert's answer
2022-03-21T12:40:13-0400
class String
{
public:
	String();
	String(int size)
	{
		this->length = size;
	}
	String(char* str)
	{
		this->data = str;
	}
	String(const String& str);
	~String();
	int strLength()
	{
		return length;
	}
	void clear()
	{
		system("cls");
	}
	bool empty()
	{
		return 0;
	}
	int charAt(char c)
	{
		return c;
	}
	char* getdata()
	{
		return data;
	}
	bool equals(char* str)
	{
		return str;
	}
	bool equalsIgnoreCase(char* str)
	{
		return str;
	}
	char* substring(char* substr, int startIndex);
	char* substring(char* substr, int startIndex, int endIndex);
	void print()
	{
		cout << data << endl;
	}


private:
	char* data;
	int length;


};

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog