// ***************************************************************** // Author: Zhixiang Chen // Class: CSCI/CMPE 1170, Spring 2009 // Lab 5: Strings, C-strings, and string operations // Date: January 12, 2009 // Comment: The code here is meant to be revised. // //----------------------------------------------------------------- // // This lab exercise is to play with strings, C-strings, and simple string // operations such as copying and concatenation. // // // Compile and run your program. When everything is fine, // print your .cpp file and turn it to me or the TA. // ***************************************************************** #include < iostream > #include #include < string > #include < cstring > #include < cmath> #include #include using namespace std; int main( ) { //declare string variables string line1, line2, line3; //three string objects char str1[50], str1[50], str3[50], //three arrays of 100 charaters str3[5], str4[5]; //two arrays of 5 characters //get string inputs cout<<"Enter one string without spaces => "; cin>> line1; //cin cannot get spaces cout>>"Enter a line with spaces => "; getline(cin, line2, '\n'); //can spaces //string concatenation line3 = line1 + line2; //+ to concatenate two strings cout << "line1 is " < "; cin>>str1; //cannot get spaces cout<<"Enter 'I like C++ very much.' =>"; cin.getline(str2, 50, 'v'); //can get spaces //string copy strcpy(str3, str1); //copy str1 to str3 //string concatenation strcat(str3, str2); //concatenate str2 to str3 cout<<"str1 is " <"; cin.width(5); //set to input up to 4 characters cin>>str3; cin>>setw(5)>>str4; //set to input up to 4 characters cout<<"str3 is " <