// MAR 02/21/00 // Written to demonstrate major capabilities of string library // plus use of some generic functions from algorithm library on strings #include #include #include using namespace std; // prototypes // tells whether a passed char is an a or not bool isA (char c); int main() { string plate1; string plate2("HGB 634"); string plate3 = "TRT 321"; string plate4(plate2); // copy constructor cout << plate1 << endl; cout << plate2 << endl; cout << plate3 << endl; cout << plate4 << endl; // look at subscripts cout << plate2[2] << endl; plate2[2] = 'X'; cout << plate2 << endl; // substring // ends with string end = plate2.substr(4,3); cout << "ends: " << end << endl; /* cout << "please enter the plate # for plate 7 followed by # then return key " << flush; string plate7; getline(cin,plate7,'#'); cout << endl << "plate7: " << plate7 << endl; */ // check empty string bool mt = plate1.empty(); if (mt) { cout << "plate1 is unknown" << endl; } else { cout << "not empty" << endl; } cout << "lengths: " << plate1.length() << " " << plate2.length() << " " << plate3.length() << " " << plate4.length() << endl; // watch - it catches the following error // plate1[5] = 'Z'; // resizing strings plate1.resize(7,'-'); // expand string and fill with dashes cout << "string " << plate1 << " has size: " << plate1.length() << endl; plate1.resize(15,'%'); // expand string and fill with percent signs cout << "string " << plate1 << " has size: " << plate1.length() << endl; plate1.resize(8); // shorten string cout << "string " << plate1 << " has size: " << plate1.length() << endl; // assignment, append, concatenate string plate5; plate5 = plate1; cout << "plates 1 and 5: " << plate1 << " " << plate5 << endl; plate1 = 'I'; cout << "plates 1 and 5: " << plate1 << " " << plate5 << endl; plate1 += " M GR8"; cout << "plates 1 and 5: " << plate1 << " " << plate5 << endl; cout << "plates 1 and 5: " << plate1 + " " + plate5 << endl; // comparison string plate6(plate1); if (plate1 > plate2) { cout << plate1 << " greater than " << plate2 << endl; } else { cout << plate1 << " NOT greater than " << plate2 << endl; } if (plate4 >= plate2) { cout << plate4 << " greater than or equal to " << plate2 << endl; } else { cout << plate4 << " NOT greater than or equal to " << plate2 << endl; } if (plate1 == plate6) { cout << plate1 << " equal to " << plate6 << endl; } else { cout << plate1 << " NOT equal to " << plate6 << endl; } // just wait so results can be seen // char wait; string wait; cout << "enter any char to continue> " << flush; cin >> wait; string plates[20]; // prepare to illustrate searching for particular substring in a string // haphazardly assign letters and numbers to plates // STUDENTS: mostly ignore assignment of plates (next 35 lines or so) char now = 'A'; // loop through first three chars for (int pos = 0; pos < 3; pos++) { // loop through all plates for (int cnt = 0;cnt < 20;cnt++){ plates[cnt] += now; // go to next letter now++; // make sure don't go past Z if (now > 'Z') { now = 'A'; } } } // add spaces for (int count = 0; count < 20; count++){ plates[count] += ' '; } int curr = '0'; // add numbers in last three positions for (int pos2 = 0; pos2 < 3; pos2++) { // loop through all plates for (int cnt = 0;cnt < 20;cnt++){ plates[cnt] += curr; // go to next letter curr++; // make sure don't go past Z if (curr > '9') { curr = '0'; } } } // show these for (int i = 0; i < 20; i++) { cout << " " << plates[i] << endl; } // just wait so results can be seen cout << "enter any char to continue> " << flush; cin >> wait; // seems to leave a \n here ?? or next one above? // I think the truck that hit me had a "XR" in the plate for (int j = 0; j < 20; j++) { int found = plates[j].find("XR"); int len = plates[j].length(); // debugging // cout << plates[j] << " found: " << found << " length: " << len << endl; if ((found > 0) && (found < len)) { cout << "Plate: " << plates[j] << " is a possibility " << endl; } } // input of string from keyboard cout << "please enter the plate # for plate 7 followed by # (then return key) " << flush; string plate7; getline(cin,plate7,'#'); cout << endl << "plate7: " << plate7 << endl; // some more things - no real tie to license plates in examples below // (just using them as strings) // insertion, removal and replacement cout << "about to play with plate 1: " << plate1 << endl; // insert starting at position 3 plate1.insert(3,"XYZ"); cout << "new plate1: " << plate1 << endl; // replace two characters starting at position 6 plate1.replace(6,2,"ABC"); cout << "new plate1: " << plate1 << endl; // our system doesn't seem to have remove // but it can be done using replace // replace 2 chars starting at position 2 with nothing plate1.replace(2,2,""); cout << "new plate1: " << plate1 << endl; // iterator - for plate 1 - set to first element in the string (container of chars) string::iterator itr = plate1.begin(); // until the iterator reaches the end of the container - display value for ( ; itr != plate1.end() ; itr++) { // * operation defined for iterators of a collection class // not frequently needed by user (client) code, because of all of // the many other capabilities provided cout << *itr << endl; } // iterators more commonly used for using generic functions // (these are in the algorithm library and are not exactly object-oriented) // e.g. reverse reverse(plate7.begin(),plate7.end()); cout << "new plate7: " << plate7 << endl; // e.g. sort sort(plate7.begin(),plate7.end()); cout << "new plate7: " << plate7 << endl; // just wait so results can be seen cout << "enter any char to continue> " << flush; cin >> wait; // a non-license plate example string statement = "There was a time, not so long ago, when programmers used Pascal, not C++"; // count number of occurrences of the char 'a' int countA; // book says that this function returns result via reference // rather than via return - but our compiler uses 3 params and returns result // ALSO - our compiler wants a function parameter - not just a value // to count - for count as well as count_if // count(statement.begin(),statement.end(),'a',countA); // countA = count(statement.begin(),statement.end(),'a'); countA = count_if(statement.begin(),statement.end(),isA); cout << "There are " << countA << " 'a's in the statement: " << endl << " " << statement << endl; // replace occurences of 'e' with 'E' replace(statement.begin(),statement.end(),'e','E'); cout << "New statement: " << endl << " " << statement << endl; // find first occurence of 't' // I don't find this find that necessary for strings because of the string // class find functions (plus generic count function) string::iterator firstTitr = find(statement.begin(),statement.end(),'t'); // show rest of string // until the iterator reaches the end of the container - display value for ( ; firstTitr != statement.end() ; firstTitr++) { cout << *firstTitr << "|"; } cout << endl; // transform to all lower case string result(statement); // initialize with source so string contains // enough elements // iterators bound the source and start the target // tolower function is used to transform each given char transform(statement.begin(),statement.end(),result.begin(), tolower); cout << "statement transformed to all lower case: " << endl << " " << result << endl; return 0; } bool isA (char c) { if (c == 'a') { return true; } else { return false; } return true; // should never execute }