/* Illustrates some of the standard C++ string functions (find, replace, erase, insert) as well as using string objects with functions and the assignment ( = ) and concatenation ( +, += ) operators. Also, const is used for arrays passed to functions whose elements should not be modified. */ #include #include string func(string, const string[], string&, string[]); void main(){ char cc[] = "A C string"; string st1("String of many words."), st2 = "many"; /* using ( ) or = to initialize a string object; these are equivalent */ int i; i = st1.find(st2); st1.replace(i,4,"few"); cout << st1 << endl; st1.erase (10,4); cout << st1 << endl; st1.insert (10,"simple "); cout << st1 << endl; st2 = cc; /* can just use = to copy a C string into a string object */ cout << st2 << endl; cout << "********************"<< endl; string s1 = "String object. ", s2[3] = {"Array of ", "three string ", "objects."}, s3; string s4 = "Modifiable string", s5[2] = {"Modifiable array", "Modifiable"}; s3 = func(s1,s2,s4,s5); cout << s3 << endl << s4 << endl << s5[0] << endl << s5[1] << endl; } string func(string ss1, const string ss2[], string& ss4, string ss5[]){ ss4 += " modified."; ss5[0] += ", element zero."; ss5[1] += " array, element one."; return (ss1 + ss2[0] + ss2[1] + ss2[2]); }