Class for STRING ARRAY

Posted: February 8, 2012 in OOP344

STRING.H

/***************************************************/
/* File     : string.h                             */
/* Writen by: Thevakaran Virutthasalam             */
/***************************************************/

#ifndef _TV_STRING_H_
#define _TV_STRING_H_

class Str{
  char* _string;
  unsigned int _size;
  void AloCopy(const Str&);
  char* Strcat(char*, const char*);
  char* Strcpy(char*, const char*);
  int   Strlen(const char*);
  int   Strcmp(const char*,const char*);

public:
  Str (unsigned int);
  Str (const Str&);
  Str& operator=(const Str&);
  Str& operator=(const char*);
  Str& operator=(const char);
  Str& operator+=(const Str&);
  Str& operator+=(const char*);
  Str& operator+=(const char);
  friend  Str operator+(const Str&, const Str&);
  friend  Str operator+(const Str&, const char*);
  friend  Str operator+(const Str&, const char);
  virtual ~Str();
  char&   Str::operator[](unsigned int);
  unsigned int size()const;
};

#endif

STRING.CPP

/***************************************************/
/* File     : string.cpp                           */
/* Writen by: Thevakaran Virutthasalam             */
/***************************************************/
#include "string.h"

void Str::AloCopy(const Str& X){
  _string = new char[X.size()+1];
  for(unsigned int i=0;i<X.size();_string[i] = X._string[i++]);
  _size = X.size();
}

Str::Str(unsigned int size){
  //Initialize the object with given size
  _string = new char[size+1];
  *_string = '\0';
  _size = size;
}

Str::Str(const Str& X){
  //Initialize the object with given of another object
  AloCopy(X);
}

Str& Str::operator=(const Str& str){
  //Assigning operator for string object
  if(this != &str){
    delete[] _string;
    AloCopy(str);
  }
  return *this;
}

Str& Str::operator=(const char* s ){ 
  //Assigning operator for null terminated string
  unsigned int size = Strlen(s);
  if(_size<size || _size>(size*3)){
    char* new_string = new char[size*2+1];
    delete[] _string;
    _string = new_string;
    _size = size*2;
  }
  Strcpy(_string, s);
  return *this;
}

Str& Str::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;
}

Str& Str::operator+=(const Str& 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;
}

Str& Str::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;
}

Str& Str::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;
}

Str operator+(const Str& lhs, const Str& rhs){
  Str copyArr = lhs;
  copyArr += rhs; //using append operator of object
  return copyArr;
}

Str operator+(const Str& lhs, const char* s){
  Str copyArr = lhs;
  copyArr += s; //using append operator of string
  return copyArr;
}

Str operator+(const Str& lhs, char c){
  Str copyArr = lhs;
  copyArr += c; //using append operator of char
  return copyArr;
}

char& Str::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 Str::size()const{
  //return the size of current object.
  return _size;
}

int Str::Strlen(const char* str){
  //String length function
  int i;
  for(i=0; str[i] != '\0'; i++);
  return i;
}

char* Str::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* Str::Strcat(char* dest, const char* srce){
	unsigned int i = Strlen(dest);
	Strcpy(dest+i, srce);
	return dest;
}

int Str::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;
}

Str::~Str(){
  //Distructor of Str object
  delete[] _string;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s