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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
#include "String2.h"
#include <string.h>
using std::cin;
using std::cout;
int String2::num_strings = 0;
//static method
int String2::HowMany()
{
return num_strings;
}
//class methods
String2::String2(const char * s)
{
len = strlen(s);
str = new char[len+1];
std::strcpy(str, s);
num_strings++;
}
String2::String2()
{
str = new char [1];
str[0] = '\0';
len = 4;
num_strings++;
}
String2::String2(const String2 & st)
{
num_strings++;
len = st.len;
str = new char[len+1];
std::strcpy(str, st.str);
}
String2::~String2()
{
--num_strings;
delete [] str;
}
void String2::stringlow()
{
char * temp;
temp = new char[len + 1];
strcpy(temp, str);
for(int iii = 0; iii < len; iii++)
if(isalpha(temp[iii]))
temp[iii] = tolower(temp[iii]);
strcpy(str, temp);
}
void String2::stringup()
{
char * temp;
temp = new char [len + 1];
strcpy(temp, str);
for(int iii = 0; iii < len; iii++)
if(isalpha(temp[iii]))
temp[iii] = toupper(temp[iii]);
strcpy(str, temp);
}
int String2::cCount(char c)
{
int count = 0;
for(int iii = 0; iii < len; iii++)
if(str[iii] == c)
count++;
return count;
}
//overloaded operator methods
String2 & String2::operator=(const String2 & st)
{
if(this == &st)
return *this;
delete[] str; //REMEMBER TO DELETE WHAT THE CURRENT STRING IS POINTING TOO!!!
len = st.len;
str = new char[len + 1];
strcpy(str, st.str);
return *this;
}
String2 & String2::operator=(const char * c)
{
delete[] str;
len = std::strlen(c);
str = new char[len + 1];
std::strcpy(str, c);
return *this;
}
//combining two strings
String2 String2::operator+(String2 & st)
{
String2 temp;
temp.len = len + st.len + 1;
temp.str = new char[temp.len];
strcpy(temp.str, str);
strcat(temp.str, st.str);
return temp;
}
//Combining a string and a literal string;
String2 operator+(const char * c, String2 & st)
{
String2 temp;
temp.len = st.len + strlen(c) + 1;
temp.str = new char [temp.len];
strcpy(temp.str, c);
strcat(temp.str, st.str); //Append one string to the end of the other
return temp;
}
//read write access for non-const String
char & String2::operator[](int i)
{
return str[i];
}
//read access for const string
const char & String2::operator[](int i) const
{
return str[i];
}
//operator overloading friends
bool operator<(const String2 & st, const String2 & st1)
{
return (std::strcmp(st.str, st1.str) < 0);
}
bool operator>(const String2 & st, const String2 & st1)
{
return (st1.str < st.str);
}
bool operator==(const String2 & st, const String2 & st1)
{
return (std::strcmp(st.str, st1.str) == 0);
}
std::ostream & operator<<(std::ostream & os, const String2 & st)
{
os << st.str;
return os;
}
std::istream & operator>>(std::istream & is, const String2 & st)
{
char temp[String2::CINLIM];
is.get(temp, String2::CINLIM);
if(is)
st = temp; //THIS RETURNS THE ERROR!! :(
while(is && is.get() != '\n')
continue;
return is;
}
|