/////////////////////////////////////////////////////// // // cstrings.cpp // // Started 1/6/97 by Jeff Ondich // Last modified: 1/6/97 // // 1. Many C++ library functions, including the standard // input and output operators << and >>, assume that // character strings are stored as "null-terminated" // char arrays. So, for example, you might create // a string like so: // // char myCat[10]; // // myCat[0] = 'E'; // myCat[1] = 'm'; // myCat[2] = 'm'; // myCat[3] = 'a'; // myCat[4] = '\0'; // // The "null" character '\0' has ASCII value 0. Read // the program below, especially the "for" loop, to // make sure you understand the structure of a C++ string. // // 2. Aren't these ctype.h functions handy? // // 3. The strings library function strcat() copies // one string onto the end of another. Which parameter(s) // of strcat(), if any, get changed by strcat()? // What is the relationship between the parameters // of strcat() and the return value of strcat()? // (You can type "man strcat" at the UNIX prompt // for some help.) // // 4. The library function strcpy() copies one string // onto another. Which gets copied onto which? // What happens if you replace the line // // strcpy( secondString, firstString ); // // by the reasonable-seeming assignment statement: // // secondString = firstString; // // We'll talk later about why this works the way // it does. // //////////////////////////////////////////////////////// #include #include #include const int kMaxStringLength = 100; int main( void ) { char c; char firstString[kMaxStringLength], secondString[kMaxStringLength]; cout << "Type something: "; cin.getline( firstString, kMaxStringLength ); cout << "Type something else: "; cin.getline( secondString, kMaxStringLength ); // Loop through a string to print it out one // character at a time. cout << "Here's your first string, all upper case: "; for( int i=0; firstString[i] != '\0'; i++ ) { c = toupper(firstString[i]); cout << c; } cout << endl << endl; // Use a utility function to "concatenate" // the copy onto the original. cout << "The concatenation: " << strcat( firstString, secondString ) << endl; cout << "The first string, after concatenation: " << firstString << endl; cout << "The second string, after concatenation: " << secondString << endl << endl; // Use a utility function to copy one string to // another. cout << "Copying the new first string onto the second." << endl; strcpy( secondString, firstString ); cout << "Here's the first string: " << firstString << endl; cout << "Here's the second: " << secondString << endl << endl; return( 0 ); }