string2.h
# ifndef STRING2_H_
# define STRING2_H_
# include <iostream>
using std:: ostream;
using std:: istream;
class String {
private :
char * str;
int len;
static int num_strings;
static const int Cinlim = 80 ;
public :
String ( const char * s) ;
String ( ) ;
String ( const String & ) ;
~ String ( ) ;
int length ( ) const {
return len; }
String & operator = ( const String & ) ;
String & operator = ( const char * ) ;
char & operator [ ] ( int i) ;
const char & operator [ ] ( int i) const ;
friend bool operator < ( const String & st, const String & st2) ;
friend bool operator > ( const String & st1, const String & st2) ;
friend bool operator == ( const String & st, const String & st2) ;
friend ostream & operator << ( ostream & os, const String & st) ;
friend istream & operator >> ( istream & is, String & st) ;
static int HowMany ( ) ;
friend String operator + ( const String& s1, const String & s2) ;
String operator + ( const char * ) const ;
void StringLow ( ) ;
void StringHigh ( ) ;
const int HowManyChar ( const char & c) ;
} ;
# endif
string2.cpp
# include <cstring>
# include <cctype>
# include "string2.h"
using std:: cout;
using std:: cin;
int String:: num_strings= 0 ;
int String :: HowMany ( ) {
return num_strings;
}
String :: String ( const char * s) {
len= std:: strlen ( s) ;
str= new char [ len+ 1 ] ;
std:: strcpy ( str, s) ;
num_strings++ ;
}
String :: String ( ) {
len= 4 ;
str= new char [ 1 ] ;
str[ 0 ] = '\0' ;
num_strings++ ;
}
String :: String ( const String& st) {
num_strings++ ;
len= st. len;
str= new char [ len+ 1 ] ;
std:: strcpy ( str, st. str) ;
}
String :: ~ String ( ) {
-- num_strings;
delete [ ] str;
}
String & String:: operator = ( const String & st) {
if ( this == & st) {
return * this ;
}
delete [ ] str;
len= st. len;
str= new char [ len+ 1 ] ;
std:: strcpy ( str, st. str) ;
return * this ;
}
String & String:: operator = ( const char * s) {
delete [ ] str;
len = std:: strlen ( s) ;
str= new char [ len+ 1 ] ;
std:: strcpy ( str, s) ;
return * this ;
}
char & String:: operator [ ] ( int i) {
return str[ i] ;
}
const char & String:: operator [ ] ( int i) const {
return str[ i] ;
}
bool operator < ( const String & st1, const String & st2) {
return ( std:: strcmp ( st1. str, st2. str) < 0 ) ;
}
bool operator > ( const String & st1, const String & st2) {
return st2< st1;
}
bool operator == ( const String & st1, const String & st2) {
return ( std:: strcmp ( st1. str, st2. str) == 0 ) ;
}
ostream & operator << ( ostream & os, const String & st) {
os << st. str;
return os;
}
istream & operator >> ( istream & is, String & st) {
char temp[ String:: Cinlim] ;
is. get ( temp, String:: Cinlim) ;
if ( is) {
st= temp;
}
while ( is&& is. get ( ) != '\n' ) {
continue ;
}
return is;
}
String operator + ( const String& s1, const String & s2) {
char * temp= new char [ s1. len+ s2. len+ 1 ] ;
strcpy_s ( temp, s1. len+ 1 , s1. str) ;
strcpy_s ( temp+ s1. len, s2. len+ 1 , s2. str) ;
temp[ s1. len+ s2. len] = '\0' ;
return String ( temp) ;
}
String String:: operator + ( const char * s) const {
int length= len+ strlen ( s) ;
char * temp= new char [ length+ 1 ] ;
strcpy_s ( temp, len+ 1 , str) ;
strcpy_s ( temp+ len, std:: strlen ( s) + 1 , s) ;
temp[ length] = '\0' ;
return String ( temp) ;
}
void String :: StringLow ( ) {
for ( int i= 0 ; i< len; i++ ) {
if ( str[ i] >= 'A' && str[ i] <= 'Z' ) {
str[ i] = tolower ( str[ i] ) ;
}
}
}
void String :: StringHigh ( ) {
for ( int i= 0 ; i< len; i++ ) {
if ( str[ i] >= 'a' && str[ i] <= 'z' ) {
str[ i] = toupper ( str[ i] ) ;
}
}
}
const int String :: HowManyChar ( const char & c) {
int sum= 0 ;
for ( int i= 0 ; i< len; i++ ) {
if ( str[ i] == c) {
sum++ ;
}
}
return sum;
}
main.cpp
# include <iostream>
using namespace std;
# include "string2.h"
int main ( ) {
String s1 ( " And I am a C++ Student." ) ;
String s2= "Please enter your name: " ;
String s3;
cout<< s2;
cin>> s3;
s2= "My name is " + s3;
cout<< s2<< ".\n" ;
s2= s2+ s1;
s2. StringHigh ( ) ;
cout<< "The string\n" << s2<< "\ncontains" << s2. HowManyChar ( 'A' ) << "'A' in it.\n" ;
s1= "red" ;
String rgb[ 3 ] = {
String ( s1) , String ( "green" ) , String ( "blue" ) } ;
cout<< "Enter the name of a primary color for mixing light: " ;
String ans;
bool succees= false ;
while ( cin>> ans) {
ans. StringLow ( ) ;
for ( int i= 0 ; i< 3 ; i++ ) {
if ( ans== rgb[ i] ) {
cout<< "That's right!" << endl;
succees= true ;
break ;
}
}
if ( succees) {
break ;
} else {
cout<< "Try again!" << endl;
}
}
cout<< "bye!" << endl;
return 0 ;
}
测试结果