1001. The String Class

Source: Internet
Author: User

#include <iostream>
#include <string>
#include <cstring>
using namespace Std;
Class String {

Public

String (); str = ""

String (const char*); str = "ABC"

String (const string&); str = other_string

string& operator= (const char *);

string& operator= (const string&);

String operator+ (const string&);

~string ();

char& operator[] (int i);

Char operator[] (int i) const;

int size () const;

string& operator+= (const string&);

string& operator+= (const char*);

Friend ostream& operator<< (ostream&, const string&);

Friend istream& operator>> (istream&, string&);

friend bool operator== (const string& x, const char* s);

friend bool operator== (const string& x, const string& y);

friend bool Operator!= (const string& x, const char* s);

friend bool Operator!= (const string& x, const string& y);

Private

Char *str;

};

String::string () {
str = new CHAR[1];
str[0]= ' + ';
}


String::string (const char* a) {
int t = strlen (a) +1;
str = new Char[t];
str = strcpy (str,a);
str[t-1]= ' + ';
}

String::string (const string& b) {
int T=strlen (B.STR) +1;
str = new Char[t];
str = strcpy (STR,B.STR);
str[t-1]= ' + ';
}


string& string::operator= (const char *b) {
int T=strlen (b) +1;
str = new Char[t];
str = strcpy (STR,B);
str[t-1]= ' + ';
return *this;
}

string& string::operator= (const string& b) {
int T=strlen (B.STR) +1;
str = new Char[t];
Str =strcpy (STR,B.STR);
str[t-1]= ' + ';
return *this;
}

String string::operator+ (const string& b) {

String str1;
str1 = new Char[strlen (B.STR) +1+strlen (THIS->STR)];
for (int i=0;i<strlen (THIS->STR); i++) {
str1.str[i]=this->str[i];
}
for (int j=strlen (THIS->STR); J<strlen (B.STR) +strlen (THIS->STR); j + +) {
Str1.str[j]=b.str[j-strlen (THIS->STR)];
}
Str1[strlen (B.STR) +strlen (this->str)]= ' + ';
return str1;
}

String::~string () {
delete [] str;
}

char& string::operator[] (int i) {
return str[i];
}

Char string::operator[] (int i) const{
return str[i];
}

int string::size () const{
return strlen (str);
}

string& string::operator+= (const string& b) {

int L=strlen (str) +strlen (B.STR);
Char *p=new char[l+10];
strcpy (P,STR);
strcat (P,B.STR);
strcpy (STR,P);
return *this;
}

string& string::operator+= (const char* b) {

int L=strlen (str) +strlen (b);
Char *p=new char[l+1];
strcat (P,STR);
strcat (P,B);
strcpy (STR,P);
return *this;
}

ostream& operator<< (ostream& A, const string& b) {
a<<b.str;
return A;
}

istream& operator>> (istream& A, string& b) {
a>>b.str;
return A;
}

BOOL operator== (const string& x, const char* s) {
if (strlen (X.STR)!=strlen (s)) return false;
for (int i=0;i<strlen (X.STR); i++) {
if (X.str[i]!=s[i]) return false;
}
return true;
}

BOOL operator== (const string& x, const string& s) {
if (strlen (X.STR)!=strlen (S.STR)) return false;
for (int i=0;i<strlen (X.STR); i++) {
if (X.str[i]!=s.str[i]) return false;
}
return true;
}

BOOL Operator!= (const string& x, const char* s) {
if (strlen (X.STR)!=strlen (s)) return true;
int count=0;
for (int i=0;i<strlen (s); i++) {
if (X.str[i]!=s[i]);
count++;
}
if (Count==strlen (s)) return false;
else return true;
}

BOOL Operator!= (const string& x, const string& s) {
if (strlen (X.STR)!=strlen (S.STR)) return true;
int count=0;
for (int i=0;i<strlen (S.STR); i++) {
if (X.str[i]!=s[i]);
count++;
}
if (Count==strlen (S.STR)) return false;
else return true;
}


String F1 (String A, string b)

{

A[2] = ' x ';

char C = b[2];

cout << "in F:" << a << ' << b << ' ' << c << ' \ n ';

return b;

}

void F2 (String s, const string& R)

{

Char C1 = s[1]; C1 = s.operator[] (1). Operator CHAR ()

S[1] = ' C '; S.operator[] (1). operator= (' C ')

char C2 = r[1]; C2 = r.operator[] (1)

R[1] = ' d '; Error:assignment to Non-lvalue Char, r.operator[] (1) = ' d '

}

void F ()

{

String x, y, S;

cout << "Please enter strings\n";

Cin >> x >> y;

cout << "x=" << x << ", y =" << y << ' \ n ';

y = F1 (x, y);

cout << y << Endl;

F2 (x, y);

cout << "s = \" "<< s <<" \ "" << Endl;

s = "abc";

cout << "s = \" "<< s <<" \ "" << Endl;


cout << "\" "<< x <<" \ "+ \" "<< y <<" \ "=" << "\" "<< x+y <<" \ "\ n";

String z = x;

cout<<z<<endl;


if (x! = z) cout << "x corrupted!\n";

X[0] = '! ';

if (x = = z) cout << "Write failed!\n";

cout << "Exit:" << x << ' << z << ' \ n ';

z = s;

if (s! = z) cout << "s corrupted!\n";

S[0] = '! ';

if (s = = z) cout << "Write failed!\n";

cout << "exit:" << s << ' << z << ' \ n ';

}

int main ()

{

int T;

Cin >> T;

while (t--)

{

f ();

}

}

1001. The String Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.