/*
  Name: klasa string
  Author: Piotr Lezon 21826
  Date: 05-01-06 13:09                  
*/

#include<iostream.h>
class string{
      private:
             int roz;
             char *wsk;
      public:
             string();
             string(const char *s);
             string(const string&);
             ~string();
             void wypisz();
             int rozmiar();
             string& odwroc();
             string& operator=(const string&);
             string operator+(const string&);   
             int operator==(const string&);   
             friend ostream& operator<<(ostream&, const string&);
};

string::string()                            //konstruktor domniemany
{
   roz = 0;          
   wsk = new char[roz+1]; 
   assert(wsk != 0);
   strcpy(wsk, "");
}

string::string(const char *s)                //kontruktor z lista inicjalizacyjna
{
   roz = strlen(s);          
   wsk = new char[roz+1];
   assert(wsk != 0); 
   strcpy(wsk, s);             
}

string::string(const string &kopia)         //konstruktor kopiujacy
{
   roz = kopia.roz;
   wsk = new char[roz+1];
   assert(wsk != 0);
   strcpy(wsk, kopia.wsk);
}

string::~string()               //konstruktor domniemany
{
 delete [] wsk;
}

void string::wypisz()           //metoda wypisujaca string oraz jego dlugosc 
{
     cout<<"\""<<wsk<<"\"\t"; cout<<"Dlugosc: "<<rozmiar()<<"\n";
}

int string::rozmiar()         //metoda zwracajaca dlugosc stringa - bez znaku konca lancucha
{
    return roz;
}

string string::operator+(const string &p) //metoda laczaca dwa stringi i zapisujaca je w stringu na rzecz ktorego zostala wywolana; p - prawy string
{
   char *temp = wsk;
   roz = roz + p.roz;
   wsk = new char[roz + 1];
   assert(wsk != 0);
   strcpy(wsk, temp);
   strcat(wsk, p.wsk);
   delete [] temp;
   return *this;
}

string& string::operator=(const string &p)
{
   if (&p != this) 
   {  
      delete [] wsk; 
      roz = p.roz;
      wsk = new char[roz + 1];
      assert(wsk != 0);
      strcpy(wsk, p.wsk);
   }
   return *this;
}

ostream& operator<<(ostream &out, const string &s)
{
   out << s.wsk;
   return out;
}

string& string::odwroc()
{
       int t=roz-1;
       int i=0;
       char * w=new char[roz+1];
       assert(w != 0);
       for(t;t>=0;t--)
       {
        w[i]=wsk[t];
        
        i++;
       }
       strcpy(wsk,w);
       delete [] w;
       return *this;
}

int string::operator==(const string &s)
{ 
    return strcmp(wsk, s.wsk) == 0; 
}
int main()
{
    string a;
    cout<<"konstruktor domyslny:\t";
    a.wypisz();
    string b("Wyzsza Szkola");
    cout<<"konstruktor stand.:\t";
    b.wypisz();
    string c(b);
    cout<<"konstruktor kopiujacy:\t";
    c.wypisz();
    string d(" Informatyki i Zarzadzania");
    cout<<d<<endl;
    a=(c+d); //operator przypisania
    cout<<"Nasz string a wypisany za pomoca przeciazanego operatora \"<<\": \n"<<a<<endl; 
    cout<<a<<" i "<<b<<":";
    if(a==b) cout<<"Stringi sa identyczne\n"; else cout<<"Stringi NIE sa identyczne\n";
    a.odwroc();
    cout<<"Odwrocony string: "<<a<<endl;
    a.odwroc(); //odwracamy jeszcze raz ;)
    cout<<a<<endl;
    system("pause");
    return 0;
}
