Playing around with std::string

vegaseat 2 Tallied Votes 199 Views Share

You can have fun and learn something too. Not too much fun though! Here we take a lighthearted look at C++ string, various ways to assign a string and substring, spell forward and reverse, find characters and substrings, append, insert, replace, remove characters, separate a sentence into words and more.

// testing std::string // a Dev-C++ challenged Console Application by vegaseat 19dec2004 #include <iostream> #include <string> using namespace std; // std::string int main() { int k, n; // assign a string string s1 = "What is the difference between roast beef and pea soup?"; cout << s1 << endl << endl; // use \" for embedded double quotes string s2 = "<tr><td><img src=\"naomi.bmp\"></td><td><b>"; cout << s2 << endl << endl; // for a multiline assignment use \ to connect string s3 = "If you tell a lie long enough\n\ and often enough,\n\ it becomes the truth!"; cout << s3 << endl << endl; // assign a substring from source to target // starting at index = 8 and len = 3 characters long string target; string source = "No new taxes!"; target.assign(source, 8, 3); cout << source << endl; cout << target << endl; // displays axe // spell out the above string string::const_iterator ai; // spell forwards for(ai = source.begin(); ai != source.end(); ai++) { cout << *ai << ' '; // separate with a space } cout << endl; // spell in reverse for(ai = source.end() - 1; ai != source.begin() - 1; ai--) { cout << *ai << ' '; // separate with a space } cout << endl << endl; // assign num = 50 characters '!' to a string string hey; hey.assign(50,'!'); cout << hey << endl; // append hey to source source.append(hey); cout << source << endl << endl; // add marker for(k = 0, n = 0; k < 18; k++, n++) { if (n > 9) n = 0; cout << n; } cout << endl; string text = "There was nobody at the Missing Persons' Bureau"; cout << text << endl; // return the character at position 0 and 15 of text char ch = text.at(0); cout << "Character at position 0 = " << ch << endl; ch = text.at(15); cout << "Character at position 15 = " << ch << endl; // size() same as length() cout << "size = " << text.size() << " capacity = " << text.capacity(); cout << endl << endl; // copy num = 17 char of oldstr into newstr starting at index = 0 string oldstr = "Frank is very fatherly!"; // why not a little old stuff char newstr[80] = {0}; oldstr.copy(newstr, 17, 0); cout << oldstr << endl; cout << newstr << endl << endl; // add marker for(k = 0; k < 10; k++) { cout << k; } cout << endl; // removes num = 6 characters from the current string // starting at index = 23 string sd("A really stupid person never becomes senile!"); cout << sd << endl; sd.erase(23, 6 ); cout << sd << endl << endl; // return first occurrence of "stupid" within the current string // starting at index = 0, string::npos (-1) if nothing is found int pos = sd.find( "stupid", 0 ); if ( pos != string::npos ) { cout << "stupid is at index " << pos; } else { cout << "Didn't find stupid"; } cout << endl; // doing a reverse search from index, string::npos = fail (-1) pos = sd.rfind( "smart", 15 ); cout << "smart is at index " << pos << " (fail=-1)" << endl << endl; // inserts str2 into str1 at locations 11 and then 20 string str1 = "Judges live on income"; string str2 = " fixed"; str1.insert(11, str2); str1.insert(20, str2); cout << str1 << endl; // add ! to the end str1.push_back('!'); cout << str1 << endl << endl; // replace characters of tooth string with num (here length) // characters from teeth string, beginning at index 21 string tooth = "He ate it all with a toothpick"; cout << tooth << endl; string teeth = "teethpick"; tooth.replace( 21, teeth.length(), teeth ); cout << tooth << endl << endl; // return a substring of a string starting at index = 19 string str("They will greet us with flowers!"); string sub = str.substr(19); cout << "original string : " << str << endl; cout << "substring from 19 on : " << sub << endl; // change the size of string to num = 18 characters str.resize(18); cout << "resize to 18 char : " << str << endl; // dito, but pad excess (here past 18) with char '!' str.resize(21,'!'); cout << "resize with ! padding: " << str << endl << endl; // swap strings string first( "one, uno, eins, interesting lines" ); string second( "two, dos, zwei, cake-flavored pie" ); cout << "Before swap:" << endl; cout << "first = " << first << endl; cout << "second = " << second << endl << endl; first.swap( second ); cout << "After swap:" << endl; cout << "first = " << first << endl; cout << "second = " << second << endl << endl; // use at() and substr() to separate a string s1 into words cout << s1 << endl << endl; // show s1 in case you forgot int start = 0; // add a space to the end of s1 since this is our delimiter // this allows us to catch the last word properly s1.push_back(' '); for(k = 0; k < s1.size(); k++) { // found a space now show the word before that if (s1.at(k) == ' ') { // substr(startposition, length) cout << s1.substr(start, k - start) << endl; // update to new startposition start = k + 1; } } cout << endl; cout << "Anyone can roast beef!" << endl; cin.get(); // wait return 0; }
close