class String
{
char* pData;
public:
String(); //default constructor
~String(); //default destructor
String( String&); //copy constructor
const String& operator=( String&); //overloaded assignment operator
void operator==( const String&); //overloaded compare operator
String operator+( String); //overloaded concat operator
void operator[]( int); //index operator
String( char*); //one argument operator
char* GetBuffer() { return pData;} //to get pData
inline int Size(); //to find the length of pData;
void ToUpper(); //convert to uppercase
void ToLower(); //convert to lowercase
void SubString( int, int); //to get a sub string
void LeftTrim(); //to trim spaces on left
//void RightTrim(); //to trim spaces on right
};
String::String()
{
//cout << "Constructor";
pData = NULL;
}
String::~String()
{
//cout << "Destructor";
delete pData;
}
String::String( String& strn1)
{
//cout << "Copy Constructor" << endl;
int len1;
len1 = strn1.Size();
if ( pData)
delete pData;
pData = new char[len1];
strcpy( pData, strn1.pData);
}
const String& String::operator=( String& strn2)
{
//cout << "Overloaded = Operator" << endl;
int len2;
if( this == &strn2)
return *this;
len2 = strn2.Size();
if( pData)
delete pData;
pData = new char[len2];
strcpy( pData, strn2.pData);
return *this;
}
void String::operator==( const String& strn3)
{
//cout << "Inside ==";
if( *strn3.pData == *pData)
cout << "The strings match" << endl;
else
cout << "The strings dont match" << endl;
}
String String::operator+( String strn4)
{
int len3, plen, totlen;
len3 = strn4.Size();
plen = Size();
totlen = len3 + plen;
char* temp = new char[totlen];
for ( int i = 0; i < plen; i++)
{
temp[i] = pData[i];
}
if( pData)
delete pData;
pData = new char[totlen];
for ( int i = 0; i < len3; i++)
{
temp[plen + i] = strn4.pData[i];
}
String newtemp( temp);
return newtemp;
/*
for ( int i = 0; i < totlen; i++)
{
cout << temp[i];
} */
cout << endl;
}
void String::operator[]( int posi)
{
char ch;
ch = pData[posi - 1];
cout << ch << endl;
}
String::String( char* strn5)
{
//cout << "One arg Constructor" << endl;
pData = strn5;
}
inline int String::Size()
{
return strlen(pData);
}
void String::ToUpper()
{
int len4;
len4 = Size();
char* temp = new char[len4];
temp = pData;
char* ch = new char[len4];
for( int i = 0; i < len4; i++)
{
ch[i] = toupper(*temp);
temp++;
}
for( int i = 0; i < len4; i++)
{
cout << ch[i];
}
}
void String::ToLower()
{
int len5;
len5 = Size();
char* ch = new char[len5];
for ( int i = 0; i < len5; i++)
{
ch[i] = tolower(*pData);
pData++;
}
for( int i = 0; i < len5; i++)
{
cout << ch[i];
}
}
void String::SubString( int pos, int range)
{
int len6;
len6 = Size();
char* str = new char[len6];
str = pData;
for( int i = pos - 1; i < range + pos - 1; i++)
{
cout << str[i];
}
cout << endl;
}
void String::LeftTrim()
{
int count = 0, len7;
len7 = Size();
char *pTmp = new char[Size()];
pTmp = pData;
if( pData)
delete pData;
for( int i = 0; i < len7; i++)
{
if( *pTmp == ' ')
{
*pTmp++;
count++;
}
}
for( int i = 0 ; i < len7 - count; i++)
{
cout << pTmp[i];
}
cout << endl;
}
void String::RightTrim()
{
int count = 0, len8;
len8 = Size();
char *pTmp = new char[Size()];
pTmp = (pData + len8);
if( pData)
delete pData;
for( int i = len8; i > 0; i--)
{
if( *(pTmp - 1) == ' ')
{
*pTmp--;
count++;
}
}
char *pnewTmp = new char[Size()];
pnewTmp = pTmp;
if( pTmp)
delete pTmp;
for( int i = 0; i < len8; i++)
{
cout << pnewTmp[i];
}
cout << endl;
}
neologic 0 Newbie Poster
Narue 5,707 Bad Cop Team Colleague
neologic 0 Newbie Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
Stoned_coder 6 Junior Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.