
-----------------------------------
tinhnho
Tue Feb 24, 2004 11:25 pm

Please help with String
-----------------------------------
Write the following member function:
// strcmp is negative if s < s1
// is 0 if s == s1
// and positive if s > s1
// where s is the emplicite argument 

int my_string::strcmp(const my_string& s1);


// strrev reverses the my_string
void my_string::strrev();


//print oeverloaded to print the frist n characters
void my_string:: print(int n);


here is what i got so far:

#include 
#include 
#include 


using namespace std;

class my_string 
{
public:
   my_string();
   my_string(const my_string& str);
   my_string(const char* p);
   ~my_string();

   int strcmp(const my_string& s1);
   void strrev();
   void print(int n);

   int const getLen();
   const char* getS();
private:
      char* s;
      int len;
};


my_string::my_string(): len(0),s (0){}

my_string::my_string(const char* p)
{
   len = strlen (p);
   s = new char[len +1];
   assert(s !=0);
   strcpy(s, p);
}


my_string::my_string(const my_string& str):len(str.len)
{
   s = new char[len+1];
   assert(s !=0);
   strcpy(s, str.s);
}


// Destructor
my_string::~my_string() 
{ 
   delete [] s;
}


int my_string::strcmp(const my_string& s1)
{
   int i;
   for(i=0; s1.s[i] && s[i] && s1.s[i] == s[i]; ++i)
   {

      return s1.s[i]-s[i];
   }
}


void my_string::strrev()
{
    my_string strrev;
    char* temp = new char[len + 1];
    int i= 0;
    int j= 0;
    while (s[i])
    {
       temp[j++] = s[i--];
    }
    temp[j] ='\0';
    strcpy(s,temp);
    delete[] temp;
}


void my_string::print(int n)
{

   return ;
}


int const my_string::getLen()
{
   return len;
}


const char* my_string::getS()
{
   return s;
}


int main ()
{

   my_string myStr ("same");

   if (myStr.strcmp ("same") < 0)
      cout  len || n == 0) n = len;
   for (int i = 0; i < n; ++i)
   cout 