# Pragma warning (Disable: 4996) # Include "string. H" # Include <string. h> // # Include <iostream> // Using namespace STD;
String: string (const char * Str) { STR _ = allocandcpy (STR ); }String: string (const string & other) { STR _ = allocandcpy (other. Str _); } String & string: Operator = (const string & other) { If (this = & other) Return * this; Return assign (other. Str _); } String & string: Operator = (const char * Str) { Return assign (STR ); } String & string: Assign (const char * Str) { Delete [] STR _; STR _ = allocandcpy (STR ); Return * this; } Bool string: Operator! () Const { Return strlen (STR _)! = 0; } Char & string: operator [] (unsigned int index) { // Return STR _ [Index]; // The non-const version calls the const version.
Return const_cast <char &> (static_cast <const string &> (* This) [Index]); } Const char & string: operator [] (unsigned int index) const { Return STR _ [Index]; } String ::~ String () { Delete [] STR _; } Char * string: allocandcpy (const char * Str) { Int Len = strlen (STR) + 1; Char * newstr = new char [Len]; Memset (newstr, 0, Len ); Strcpy (newstr, STR ); Return newstr; } Void string: Display () const { Cout <STR _ <Endl; } Int string: length () const { Return strlen (STR _); } Bool string: isempty () const { Return length () = 0; } String operator + (const string & S1, const string & S2) { // Int Len = strlen (s1.str _) + strlen (s2.str _) + 1; // Char * newstr = new char [Len]; // Memset (newstr, 0, Len ); // Strcpy (newstr, s1.str _); // Strcat (newstr, s2.str _); // // String TMP (newstr ); // Delete newstr; String STR = S1; STR + = S2; Return STR; } String & string: Operator + = (const string & other) { Int Len = strlen (STR _) + strlen (other. Str _) + 1; Char * newstr = new char [Len]; Memset (newstr, 0, Len ); Strcpy (newstr, STR _); Strcat (newstr, other. Str _); Delete [] STR _; STR _ = newstr; Return * this; } Ostream & operator <(ostream & OS, const string & Str) { OS <Str. Str _; Return OS; } Istream & operator> (istream & is, string & Str) { Char TMP [1024]; Cin> TMP; STR = TMP; Return is; } |