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;
}
}
|