String Class


Home                Programs Home

This is a simplistic implementation of a String Class called 'tstring'. It does not contain a sample program to demonstrate its usage. However, you can declare string like any other built-in type object, and use it normally like and other built-in type. This class had the + operator overloaded. Therefore, you can concatenate 2 or more string objects like any other built-in type. I have also provided the overloaded version of the ostream output indirection operator<<, so that users can directly output it to any output stream derived form ostream, without having to mess around with the implementation of the representation itself. There are other auxillary functions that may not be as efficient as they should be. These include functions for getting the length of the string, for getting the number of times a certain character is repeated, for counting the number of vowels and words in the given string. Note: The word counting algorithm is kind of pretty primitive, in the sense that it counts the number of words indirectly, by adding 1 to the number of spaces in the string. This method might not always work if the user decides to put more than 1 space between 2 or more words.


You can copy the code between the 2 HORIZONTAL LINES, paste it and save it as ASCII test, and compile it with a standards compliant C++ compiler such as g++.


#include <cstring>
#include <cctype>


#ifndef TSTRING_INTERFACE
#define TSTRING_INTERFACE


class tstring {
private:
char* str_rep;
int len;
static const char *vowel_list;
void assign_rep (const char* ch)
{
if (ch)
{
len = strlen (ch);
str_rep = new char [len + 1];
strcpy (str_rep, ch);
}
else
{
len = 0;
str_rep = reinterpret_cast<char*>(0);
}

}

int get_char_count (char ch);


public:

tstring (const char* ch = 0);
int length (void);
int words (void);
int vowel_count (void);
int char_repeated (const char c_rep);

void reserve (int size)
{
if (size > len)
{
char *temp = new char[len + 1];
strcpy (temp, str_rep);
delete[] str_rep;
str_rep = new char[size];
strcpy (str_rep, temp);
delete[] temp;
}
}


tstring (const tstring& ts);
tstring& operator= (const char* cch);
tstring& operator= (const tstring& ts);
friend tstring operator+ (const tstring&, const tstring&);
friend ostream& operator<< (ostream&, const tstring&);
~tstring ();

};

ostream& operator<< (ostream& out, const tstring& ts)
{
if (ts.str_rep)
out<<ts.str_rep;

return (out);
}






tstring operator+ (const tstring& ts1, const tstring& ts2)
{
tstring temp (ts1);
temp.reserve (ts1.len + ts2.len + 1);
strcat (temp.str_rep, ts2.str_rep);
return (temp);
}





const char *tstring::vowel_list = "aeiou";


tstring::tstring (const char* ch)
{
assign_rep (ch);
}


tstring::~tstring ()
{
if (str_rep != 0)
delete[] str_rep;

//str_rep = 0;
}


int tstring::length (void)
{
return (len);
}

int tstring::words (void)
{
return (get_char_count (' ') + 1);
}

//NOTE: This routine is slightly inefficient, becuse it makes use of get_char_count
//which which is a case sensttive search routine, so we have to explicitly deal with
//comparison of the Upper Case Vowels.
int tstring::vowel_count (void)
{
int i, count = 0;

for (i = 0; i < 5; i++)
{
count += get_char_count (vowel_list[i]);
count += get_char_count (toupper (vowel_list[i]));
}

return (count);
}


//NOTE: get_char_count is a function that is case sensitive in the
//character comparisons.
int tstring::get_char_count (char ch)
{
int i;
int count = 0;

for (i = 0; i < len; i++)
{
if (ch == str_rep[i])
count++;
}

return (count);

}


int tstring::char_repeated (const char c_rep)
{
return (get_char_count (c_rep));
}



tstring::tstring (const tstring& ts)
{
assign_rep (ts.str_rep);
}





tstring& tstring::operator= (const char* cch)
{
if (str_rep == 0)
{
assign_rep (cch);
}
else if (cch != str_rep)
{
delete[] str_rep;
assign_rep (cch);
}


return (*this);

}



tstring& tstring::operator= (const tstring& ts)
{
return (*this = ts.str_rep);
}


#endif

















Hosted by www.Geocities.ws

1