Saturday, February 11, 2012
MOVED TO WORDPRESS....
Sorry...., I'm moved to wordpress since they are support for cpp code post. So please visit to http://thevakaran.wordpress.com/
Thursday, February 9, 2012
STRING ARRAY CLASS
STRING.H
STRING.CPP
/***************************************************/ /* File : string.h */ /* Writen by: Thevakaran Virutthasalam */ /***************************************************/ #ifndef _TV_STRING_H_ #define _TV_STRING_H_ class StrArr{ char* _string; unsigned int _size; void AloCopy(const StrArr&); public: StrArr (unsigned int); StrArr (const StrArr&); StrArr& operator=(const StrArr&); StrArr& operator=(const char*); StrArr& operator=(const char); StrArr& operator+=(const StrArr&); StrArr& operator+=(const char*); StrArr& operator+=(const char); friend StrArr operator+(const StrArr&, const StrArr&); friend StrArr operator+(const StrArr&, const char*); friend StrArr operator+(const StrArr&, const char); virtual ~StrArr(); char& StrArr::operator[](unsigned int); unsigned int size()const; }; char* Strcat(char*, const char*); char* Strcpy(char*, const char*); int Strlen(const char*); int Strcmp(const char*,const char*); #endif
/***************************************************/ /* File : string.cpp */ /* Writen by: Thevakaran Virutthasalam */ /***************************************************/ #include "string.h" void StrArr::AloCopy(const StrArr& X){ _string = new char[X.size()+1]; for(unsigned int i=0;i
_size = X.size(); } StrArr::StrArr(unsigned int size){ //Initialize the object with given size _string = new char[size+1]; *_string = '\0'; _size = size; } StrArr::StrArr(const StrArr& X){ //Initialize the object with given of another object AloCopy(X); } StrArr& StrArr::operator=(const StrArr& str){ //Assigning operator for string object if(this != &str){ delete[] _string; AloCopy(str); } return *this; } StrArr& StrArr::operator=(const char* s ){ //Assigning operator for null terminated string unsigned int size = Strlen(s); if(_size (size*3)){ char* new_string = new char[size*2+1]; delete[] _string; _string = new_string; _size = size*2; } Strcpy(_string, s); return *this; } StrArr& StrArr::operator=(const char c){ //Assigning operator for a single char if(_size < 2 || _size > 100) { //Checking to add/remove enough memory. delete [] _string; char* new_char = new char[11]; _size = 10; _string = new_char; } *_string= c; *(_string+1)='\0'; return *this; } StrArr& StrArr::operator+=(const StrArr& X){ //Appending operator for string object unsigned int i; unsigned int j; char* new_string = new char[X.size()+_size+1]; for(i=0;_string[i]!='\0' && i<_size;new_string[i]=_string[i++]); for(j=0;X._string[j]!='\0' && j<=X.size();new_string[j+i]=X._string[j++]); new_string[j+i]='\0'; delete[] _string; _size += X.size(); _string = new_string; return *this; } StrArr& StrArr::operator+=(const char* s){ //Appending operator for null terminated string unsigned int i; unsigned int j; unsigned int len=Strlen(s); char* new_string = new char[len+_size+1]; for(i=0;_string[i]!='\0' && i<_size;new_string[i] = _string[i++]); for(j=0;s[j]!='\0';new_string[j+i] = s[j++]); new_string[j+i]='\0'; delete[] _string; _size += len; _string = new_string; return *this; } StrArr& StrArr::operator+=(const char c){ //Appending operator for a sigle char unsigned int i = Strlen(_string); if (i+1<_size) { char* new_string = new char[_size+2]; for(i=0;_string[i]!='\0' && i<_size;new_string[i] = _string[i++]); new_string[i++]=c; new_string[i]='\0'; delete[] _string; _size += 1; _string = new_string; } else { _string[i]=c; _string[i+1]='\0'; } return *this; } StrArr operator+(const StrArr& lhs, const StrArr& rhs){ StrArr copyArr = lhs; copyArr += rhs; //using append operator of object return copyArr; } StrArr operator+(const StrArr& lhs, const char* s){ StrArr copyArr = lhs; copyArr += s; //using append operator of string return copyArr; } StrArr operator+(const StrArr& lhs, char c){ StrArr copyArr = lhs; copyArr += c; //using append operator of char return copyArr; } char& StrArr::operator[](unsigned int index){ //Index operator to ruturn char at given index position //of the string if(index >= _size){ char* new_string = new char[index*2 + 1]; for(unsigned int i=0;i<_size;new_string[i] = _string[i++]); delete[] _string; _size = index*2; _string = new_string; } return _string[index]; } unsigned int StrArr::size()const{ //return the size of current object. return _size; } int Strlen(const char* str){ //String length function int i; for(i=0; str[i] != '\0'; i++); return i; } char* Strcpy(char* dest, const char* source){ //String copy function int i; for(i=0;source[i]!='\0';i++){ dest[i] = source[i]; } dest[i]='\0'; return dest; } char* Strcat(char* dest, const char* srce){ unsigned int i = Strlen(dest); Strcpy(dest+i, srce); return dest; } int Strcmp(const char* str1, const char* str2){ //String compare function int result=0; for(int i=0; !result && str1[i] != '\0' && str2[i] != '\0'; i++){ if(str1[i] > str2[i]){ result = 1; } else if(str1[i] < str2[i]){ result = -1; } } return result; } StrArr::~StrArr(){ //Distructor of StrArr object delete[] _string; }
Wednesday, February 1, 2012
Subscribe to:
Posts (Atom)