/* This program demonstrates * C++ string objects * * C++ offers a string class that makes it easier to work with strings. * The string class let's us use "string" as a user defined composite data * type. We can create variables of type string. * The biggest advantage of the string class is memory management. We do not * have to check for bounds. C++ handles resizing the array for us. * Internally a string object is still a CString, but C++ has just provided * us with tools to make it easier for us. * Here, we will demonstrate the 4 CString functions we learned about, using * C++ string objects. * To copy, we use the = sign instead of the strcpy function. * To get the length, we use the size function or length function instead of strlen * To concatenate, we use the += operator or the append function instead of strcat * To compare, we can just use the relational operators (<, <=, >, >=, ==, !=) * instead of strcmp. Comparison is still done in lexicographic order, but * it is easier to read. * * */ #include #include #include using namespace std; void sort(char st[][100], int num); int sentenceCount(string str); int main() { /* C++ defines "string" as the user-defined data type for a c++ string object * As we can see, we can use the same syntax we are used to with the numeric * data types. */ string st1 = "Hello World"; cout<<"st1: "<> would work on a string object, but it would only read until the first whitespace. * We should use getline to read in a multi-word string. * The syntax for getline is slightly different for string objects. * We start off with getline, move the cin inside as a parameter, and * pass in the string variable and the terminating character. * We leave out the size because C++ handles the size for us. * As usual, the terminating character is aby default, the newline, * We need a cin.ignore() between a non-getline input and a getline input. */ cout<<"Enter some text: "; getline(cin, st1, '\n'); cout<<"You entered: "< st2) cout<