/* This program demonstrates * 1. Arrays of c-strings * 2. C++ string objects * * An array of chars is a C-String. We can treat it as a regular array * along with all the special properties that apply to C-strings alone * Naturally, we can extend this idea into an array of C-Strings, which is * an array of arrays of chars - a 2 dimensional array of chars. * Here, each array of chars (an entire row of the array) can be treated as * a CString. Then, we only need to specify the row for the array. * We can also refer to each character individually. Then, we need to specify * both the row and the column. * * 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() { /* An array of C-strings is essentially a two dimensional array of chars, where * each "row" can be considered a CString on its own. */ char names[10][100]; cout<<"Enter 10 names: "; for(int i=0; i<10; i++) cin.getline(names[i], 100); sort(names, 10); cout<<"The sorted names are: "<> 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< 0) { strcpy(temp, arr[j]); strcpy(arr[j], arr[j+1]); strcpy(arr[j+1],temp); } } } } /* We can index into the internal c-string of a c++ string object using the * index-of operator []. * This part works just like a C-string * This is the sentenceCount function we wrote for a C-string, adapted for * C++ string objects. */ int sentenceCount(string st) { int count = 0; for(int i=0; i< st. size(); i++) { if( st[i] =='.' || st[i] =='?' || st[i] =='!') count++; } return count; }