/*************************************************************\
Written By: Jabbar Ganji ganji_j@yahoo.com http://g99.blogfa.com
\*************************************************************/
#include <string.h>
#include <iostream>
using namespace std;
class Str
{
char* s;
int len;
public:
Str() { s=0; len=0; }
Str(char* p, int length=-1)
{
s=0;
Set(p, length);
}
Str(int length, char fill=0)
{
s=0;
Set(length, fill);
}
Str(const Str& a) { s=0; Set(a.s, a.len); }
~Str() { if(s!=0) delete[] s; s=0; len=0; }
void Set(int length=0, char fill=0)
{
if(s!=0)
delete[] s;
if(length>0)
{
s=new char[length];
len=length;
for(int i=0; i<len; i++)
s[i]=fill;
}
else
{
len=0;
s=0;
}
}
void Set(char* p, int length=-1)
{
if(length<=0)
len=strlen(p);
else
len=length;
if(s!=0)
delete[] s;
s=new char[len];
int i;
for(i=0; i<len; i++)
s[i]=p[i];
}
Str& operator= (const Str a)
{
Set(a.s, a.len);
return *this;
}
int Len() { return len; }
friend ostream& operator<<(ostream& u, Str a)
{
int i;
for(i=0; i<a.len; i++)
u << a.s[i];
return u;
}
friend istream& operator>>(istream& u, Str& a)
{
int n;
u >> n;
a.Set(n);
for(int i=0; i<a.len; i++)
u >> a.s[i];
return u;
}
bool operator==(const Str& a)
{
int i;
if(len!=a.len)
return false;
for(i=0; i<len; i++)
if(s[i]!=a.s[i])
return false;
return true;
}
bool operator!=(const Str a)
{
return !(*this==a);
}
bool operator<(const Str a)
{
int i;
for(i=0; i<len && i<a.len; i++)
{
if(s[i]<a.s[i])
return true;
if(s[i]>a.s[i])
return false;
}
if(len<a.len)
return true;
return false;
}
bool operator>(const Str a)
{
return !(*this<a || *this==a);
}
char operator[](int i)
{
if(i>=len || i<0)
return 0;
return s[i];
}
Str ToUpper()
{
for(int i=0; i<len; i++)
if(s[i]>'Z')
s[i]-=32;
return *this;
}
Str ToLower()
{
for(int i=0; i<len; i++)
if(s[i]<'a')
s[i]+=32;
return *this;
}
Str operator+(Str a)
{
Str t(len+a.len);
int i, j;
for(i=0; i<len; i++)
t.s[i]=s[i];
for(j=0; j<a.len; j++, i++)
t.s[i]=a.s[j];
return t;
}
Str& operator+=(Str a)
{
Str t= *this+ a;
Set(t.s, t.len);
return *this;
}
};
