You are on page 1of 3

#include

#include
#include
#include

<conio.h>
<iostream.h>
<string.h>
<stdlib.h>

class String{
char *sptr;
public:
//constructor implicit
String () {sptr=NULL;}
//constructor de copiere
String (const String &s){
if (!s.sptr) sptr=NULL;
else{
sptr = new char[strlen(s.sptr)+1];
strcpy(sptr, s.sptr);
}
}
//constructor de transformare de tip
String (const char *s){
if (!s) sptr=NULL;
else{
sptr = new char[strlen(s)+1];
strcpy(sptr, s);
}
}
//destructor
~String () {delete[] sptr;}
//lungimea sirului
int Length (){
if (!sptr) return 0;
else return strlen(sptr);
}
//returnarea sirului
char* getString (){
if (!sptr) return "Null";
else return sptr;
}
//setarea
void setString (char *s){
delete[] sptr;
if (!s) sptr=NULL;
else{
sptr = new char[strlen(s)+1];
strcpy(sptr,s);
}
}
//atribuirea
void Atrib (const String &s){
delete[] sptr;
if (!s.sptr) sptr=NULL;
else{
sptr = new char[strlen(s.sptr)+1];

strcpy(sptr, s.sptr);
}
}
//compararea a doua siruri
int Compare (const String &s){
return strcmpi(sptr, s.sptr);
}
//apartenenta unui subsir in alt sir
int Search (const String &s){
if (strstr(s.sptr,sptr)) return 1;
else return 0;
}
};
void main(){
clrscr();
char s[200];
String s1;
String s2("C++ is the best");
String s3(s2);
//-------------------------------------------------------------------------------------------------------cout << "Sirurile initiale:" << endl;
cout << "s1:" << s1.getString() << endl;
cout << "s2:" << s2.getString() << endl;
cout << "s3:" << s3.getString() << endl;
//-------------------------------------------------------------------------------------------------------cout << "\nIntroduceti sirul s1:" << endl;
cin >> s;
s1.setString(s);
s2.setString("Information Technology");
//-------------------------------------------------------------------------------------------------------cout << "\nSirurile setate:" << endl;
cout << "s1:" << s1.getString() << endl;
cout << "s2:" << s2.getString() << endl;
cout << "s3:" << s3.getString() << endl;
//-------------------------------------------------------------------------------------------------------cout << "\nLungimile sirurilor:" << endl;
cout << "lungimea s1:" << s1.Length() << endl;
cout << "lungimea s2:" << s2.Length() << endl;
cout << "lungimea s3:" << s3.Length() << endl;
//-------------------------------------------------------------------------------------------------------cout << "\nEfectuam atribuirea s1=s2:" << endl;
s1.Atrib(s2);
cout << "s1:" << s1.getString() << endl;
cout << "s2:" << s2.getString() << endl;
cout << "s3:" << s3.getString() << endl;
//-------------------------------------------------------------------------------------------------------cout << "\nCompararea s1 si s2:" << endl;
if (!s1.Compare(s2))
cout << "\"" << s1.getString() << "\" = \""
<< s2.getString() << "\"" << endl;
else

if (s1.Length()>s2.Length())
cout << "\"" << s1.getString() << "\" > \""
<< s2.getString() << "\"" << endl;
else cout << "\"" << s1.getString() << "\" < \
""
<< s2.getString() << "\""
<< endl;
//-------------------------------------------------------------------------------------------------------cout << "\nCompararea s1 si s3:" << endl;
if (!s1.Compare(s3))
cout << "\"" << s1.getString() << "\" = \""
<< s3.getString() << "\"" << endl;
else
if (s1.Length()>s3.Length())
cout << "\"" << s1.getString() << "\" > \""
<< s3.getString() << "\"" << endl;
else cout << "\"" << s1.getString() << "\" < \
""
<< s3.getString() << "\"" << e
ndl;
//-------------------------------------------------------------------------------------------------------cout << "\nSe schimba valoarea s2: Information" << endl;
s2.setString("Information");
//-------------------------------------------------------------------------------------------------------cout << "\nApartenenta s2 la s3:" << endl;
if (s2.Search(s3))
cout << "\"" << s2.getString() << "\" apartine \""
<< s3.getString() << "\"" << endl;
else cout << "\"" << s2.getString() << "\" nu apartine \""
<< s3.getString() << "\"" << endl;
//-------------------------------------------------------------------------------------------------------cout << "\nApartenenta s2 la s1:" << endl;
if (s2.Search(s1))
cout << "\"" << s2.getString() << "\" apartine \""
<< s1.getString() << "\"" << endl;
else cout << "\"" << s2.getString() << "\" nu apartine \""
<< s1.getString() << "\"" << endl;
getch();
}

You might also like