Making own string class

Hello. I am creating a string class using primarily vectors and have created some functions to mimic some of the string functions. The following codes are my header (functions are given), cpp file with the functions, and the main (which is given to test our program).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef MYSTRING_H
#define MYSTRING_H

#include <vector>

const int NPOS = -1; // NPOS is a value returned when a searched string is not located on a mystring.

using namespace std;

class mystring
{
	private:

		vector<char> str; // str is a vector of characters that represents the string's content.

	public:

		mystring(); // default constructor that initializes mystring of zero length.
		void get(void); // member function that obtains a mystring from user input.
		void print(void) const; // member function that outputs a mystring.
		int length(void) const; // member function that outputs the length of a mystring.
		int find(const mystring& thestring) const; // member function that searches mystring for a string and returns its position.
		bool insert(int pos, const mystring& thestring); // member function that inserts a string at a position.
		bool erase(int pos, int num); // member function that erases characters from a mystring at a position.
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "mystring.h"

#include <iostream>
#include <vector>

using namespace std;

// Constructor that initializes mystring of zero length.
mystring::mystring()
{
}

// Mimics the behavior of cin << str.
void mystring::get(void)
{
	char c; // c represents a character of str.
	cin.get(c);
	// mystring does not include a space or enter key '\n',
	// so it will terminate at that point.
	while (c != '\n' && c != ' ')
			{
				str.push_back(c);
				cin.get(c);
			} 
}

// Mimics the behavior of cout << str.
void mystring::print(void) const
{
	// each character of mystring is outputted.
	for (int i = 0; i < (int) str.size(); i++)
		cout << str[i];
}

// The length, or number of characters of mystring, is returned.
int mystring::length(void) const
{
	return str.size();
}

// Searches the mystring for the content of thestring and returns the 
// position of its first occurence in mystring.
// thestring: the string being searched for.
int mystring::find(const mystring& thestring) const
{
	for (int i = 0; i < (int) str.size(); i++)
	{
		// As each character of str goes through, it checks whether
		// the character matches the first character of thestring.
		if (str[i] == thestring.str[0])
			// if they match, it checks whether the following characters
			// of str match with the remaining characters of thestring.
			for (int j = 0; j < (int) thestring.str.size(); j++)
			{
				bool is_found = true;
				// if the characters of str do not match with the remaining
				// characters of thestring, it exits this loop and continues
				// going through the remainder of str.
				if (thestring.str[j] != str[i + j])
				{
					is_found = false;
					j = thestring.str.size();
				}
				// if the characters of str do match up with the remaining
				// characters of thestring, then it returns the position.
				if (is_found)
				{
					return i; // position of thestring in str.
				}
			}
	}

	return NPOS; // if thestring is not found in str, return the value of NPOS.
}

// Inserts thestring at position pos.
// pos: position of where the insert will occur.
// thestring: the string being inserted.
bool mystring::insert(int pos, const mystring& thestring)
{
	// if pos is negative or larger then the length of str, then nothing happens and returns false.
	if (pos < 0 || pos > (int) str.size())
		return false;
	// otherwise, the insertion occurs and returns true.
	else
	{
		int s1 = str.size(); // s1 is the size of the original string.
		int s2 = thestring.str.size(); // s2 is the size of the inserted string.

		// declare a new vector of size original string plus the inserted string.
		vector<char> newvector(s1 + s2);

		// sets the initial part of the new vector equal to the first part original string before the position.
		for (int i = 0; i < pos; i++)
			newvector[i] = str[i];

		// sets the middle part of the new vector, starting at the position, equal to the inserted string.
		for (int i = 0; i < (int) thestring.str.size(); i++)
			newvector[i + pos] = thestring.str[i];

		// sets the last part of the new vector equal to the remaining part of the original string.
		for (int i = pos; i < (int) str.size(); i++)
			newvector[i + thestring.str.size()] = str[i];
		
		// set the original string vector equal to the entire new vector.
		str = newvector;

		return true;
	}
}

// Erases num characters from mystring at position pos.
// pos: position of where the erasing begins.
// num: number of characters to be erased.
bool mystring::erase(int pos, int num)
{
	// if pos is negative or bigger than or equal to mystring's length, do nothing and return false.
	if (pos < 0 || pos >= (int) str.size())
		return false;
	// if num is negative, do nothing and return false.
	else if (num < 0)
		return false;
	// otherwise, the erasure occurs and returns true.
	else
	{
		// if num would erase past the end of str, just erase all of str beginning at pos.
		if ( (pos + num) > (int) str.size() ) 
		{
			for (int i = pos; i < (int) str.size(); i++)
				str.pop_back();
		}
		// otherwise, perform the specified erasure.
		else
		{
			// shift the characters that will not be erased over to the left,
			// into some of the slots of the characters that will be erased were.
			for (int i = pos; i < ((int) str.size() - num); i++)
				str[i] = str[i + num];

			// delete the empty slots that were left after the shift.
			for (int i = 0; i < num; i++)
				str.pop_back();
		}

		return true;
	}
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include "mystring.h"

using namespace std;

///////////////////////////////////////////////////////////////////

int main()
{
   mystring str1;
   mystring str2;
   mystring str3;

   cout << "Input a mystring: ";
   str1.get();

   cout << "You entered: ";
   str1.print();
   cout << ", its length is: " << str1.length() << "\n";

   ////////////////////////////////////////////////////

   cout << "Testing invalid insert: ";

   if (str1.insert(str1.length() + 1, str1) || str1.insert(-1, str1))
      cout << "Fail.\n";  // if your program works, you shouldn't see this.
   else
   {
      cout << "Success if your input mystring was: ";  
      str1.print();
      cout << "\n";
   }

 
   ////////////////////////////////////////////////////

   cout << "Testing invalid erase:  ";

   if (str1.erase(-1, 1) || str1.erase(str1.length(), 1) || str1.erase(0, -1))
      cout << "Fail.\n";  // if your program works, you shouldn't see this.
   else
   {
      cout << "Success if your input mystring was: ";  
      str1.print();
      cout << "\n";
   }

   ////////////////////////////////////////////////////

   cout << "Testing over-erasure:   ";

   if (str1.length() == 0)
      cout << "Empty mystring, can't test over-erasue\n";
   if (!str1.erase(str1.length() - 1, 2))
      cout << "Fail.\n";  // if your progrom works, you shouldn't see this.
   else
   {
      cout << "Last character should be erased: ";
      str1.print();
      cout << "\n";
   }

   ////////////////////////////////////////////////////

   cout << "Testing search and replace:\n";
   cout << "Input a mystring: ";
   str1.get();

   cout << "Search for: ";
   str2.get();

   int found = str1.find(str2);

   if (found == NPOS)
      cout << "Not found... trying to replace should give error message:\n";
   else
      cout << "Found at position: " << found << "\n";

   cout << "Replace with: ";
   str3.get();

   if (str1.erase(found, str2.length()))
   {
      cout << "After erasure, the mystring is: ";
      str1.print();
      cout << "\n";

      str1.insert(found, str3);
      cout << "After insertion, the mystring is: ";
      str1.print();
      cout << "\n";
   }
   else
      cout << "Invalid erase position\n";

   return 0;
}


Here are the runs of my program and also the correct program.
My output:
http://i1123.photobucket.com/albums/l557/ballin1432/exhibita.jpg
Correct output:
http://i1123.photobucket.com/albums/l557/ballin1432/exhibitb.jpg

From this, my program does not correctly replicate what it is suppose to do. My program finds the position of the first inputted mystring, instead of the second inputted mystring. Also, my program adds in the "hell", which is the string with the last character erased, to the front of the final outputs after the erasure and the insertion. Could someone explain why this happens? I really appreciate the help.
In mystring::get you probably want to clear out the old string data before adding new.
str.clear();
Thanks! I totally forgot about that. It fixed the problem.
Topic archived. No new replies allowed.