Ad

Friday, June 22, 2007

A Multi-Purpose String Class in C++

NOTE: Here I present you with a String Class in C++. We have pre-defined string class (CString in Microsoft Visual C++) which has similar but more powerful to the one presented here but using something is one thing and learning how it works is another. Here I show you how string functions actually work. All the functions are programmed from the scratch without using any other standard library function. Look at the program (Class) carefully and try to understand how each of the function is working.
  //-----------------------------
  //-----------myClass-----------
  //----A String Class in C++----

  #include<iostream.h>
  #include<stdlib.h>

  class myString
  {
   private:

   // these functions are not
   // needed outside the class
   void allocate(int);
   void copy(char *, char *);

   public:
   char *string;

   // member functions
   myString();
   myString(char *);
   int getLength();
   int getLength(char *);
   void empty();
   bool isEmpty();
   void putString(char *);
   char *getString();
   char *fromLeft(int);
   char *fromRight(int);
   char *fromMid(int, int);
   ~myString();
  };

  //--------------------------------
  //------------MEMBER--------------
  //---FUNCTION DEFINITION STARTS---
  void myString::allocate(int size)
  {
   empty();

   string=new char[size];
   if(string==NULL) exit(0);
  }

   void myString::copy(char *str1, char *str2)
   {
    int length=getLength(str2);

    for(int i=0;i<=length;i++)
      str1[i]=str2[i];
   }

   void myString::empty()
   {
    if(string!=NULL)
      {
       delete []string;
       string=NULL;
      }
   }

   bool myString::isEmpty()
   {
    if(string!=NULL)
       return false;

    return true;
   }

  int myString::getLength(char *str)
  {
   int i=0;

   while(str[i]!='\0')
     i++;

   return i;
  }

  int myString::getLength()
  {
   if(string!=NULL)
     return getLength(string);

   return -1;
  }

  myString::myString(char *str)
  {
   string=NULL;
   int size=getLength(str)+1;

   allocate(size);

   copy(string,str);
  }

  myString::myString()
  {
   string=NULL;
  }

  void myString::putString(char *str)
  {
   int size=getLength(str)+1;
 
   allocate(size);

   copy(string,str);
  }

  char *myString::getString()
  {
   if(string!=NULL)
     {
      return string;
     }
  }

  myString::~myString()
  {
   if(string!=NULL)
     delete []string;
  }
 
  char *myString::fromLeft(int chr)
  {
   if(string!=NULL)
     {
      char *temp;
      temp=new char[chr+2];

      if(temp==NULL) exit(1);

      for(int i=0;i<chr;i++)
        temp[i]=string[i];

      temp[i]='\0';
      return temp;
     }
  }

  char *myString::fromRight(int chr)
  {
   if(string!=NULL)
     {
      char *temp;
      temp=new char[chr+2];
      int a=0;

      if(temp==NULL) exit(1);

      int i=getLength()-1;
      int j=(i-chr)+1;

      for(j;j<=i;j++)
        {
         temp[a]=string[j];
         a++;
        }
   temp[a]='\0';
   return temp;
     }
  }

  char *myString::fromMid(int a,int b)
  {
   if(string!=NULL)
     {
      int size=b+1;
      char *temp;
      temp=new char[size];
      int i=a,j=0,k=(a+b);

      for(i;i<k;i++)
        {
         temp[j]=string[i];
         j++;
        }
   temp[j]='\0';
   return temp;
     }
  }
   //-----------MEMBER-------------
   //---FUNCTION DEFINITION ENDS---
   //------------------------------

  void main(void)
  {
   myString a("Learning C++ Programming");

   cout<<a.getString();
   cout<<endl;
   cout<<a.fromLeft(3);
   cout<<endl;
   cout<<a.fromRight(3);
   cout<<endl;
   cout<<a.fromMid(3,3);
   cout<<endl<<a.isEmpty();

   a.empty();
   // will not print anything
   cout<<endl<<a.fromLeft(3);
   // will print true(0)
   cout<<endl<<a.isEmpty();
  }

Good-Bye for now!

Check back for updates...

2 comments:

  1. Anonymous12:42 PM

    Not good because isEmpty returns false on 0-length string

    ReplyDelete
  2. Anonymous11:15 PM

    I have also made my own String class. here http://desireinfo.com/coding-c...

    ReplyDelete

You are free to comment anything, although you can comment as 'Anonymous' it is strongly recommended that you supply your name. Thank You.

Please don't use abusive language.