DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Code Snippet: Playing around with std::string (http://www.daniweb.com/forums/thread216379.html)

vegaseat Dec 19th, 2004 1:11 pm
Playing around with std::string
 
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.

  1. // testing std::string
  2. // a Dev-C++ challenged Console Application by vegaseat 19dec2004
  3.  
  4. #include <iostream>
  5. #include <string>
  6.  
  7. using namespace std; // std::string
  8.  
  9. int main()
  10. {
  11. int k, n;
  12.  
  13. // assign a string
  14. string s1 = "What is the difference between roast beef and pea soup?";
  15. cout << s1 << endl << endl;
  16.  
  17. // use \" for embedded double quotes
  18. string s2 = "<tr><td><img src=\"naomi.bmp\"></td><td><b>";
  19. cout << s2 << endl << endl;
  20.  
  21. // for a multiline assignment use \ to connect
  22. string s3 =
  23. "If you tell a lie long enough\n\
  24. and often enough,\n\
  25. it becomes the truth!";
  26. cout << s3 << endl << endl;
  27.  
  28. // assign a substring from source to target
  29. // starting at index = 8 and len = 3 characters long
  30. string target;
  31. string source = "No new taxes!";
  32. target.assign(source, 8, 3);
  33. cout << source << endl;
  34. cout << target << endl; // displays axe
  35.  
  36. // spell out the above string
  37. string::const_iterator ai;
  38. // spell forwards
  39. for(ai = source.begin(); ai != source.end(); ai++)
  40. {
  41. cout << *ai << ' '; // separate with a space
  42. }
  43. cout << endl;
  44. // spell in reverse
  45. for(ai = source.end() - 1; ai != source.begin() - 1; ai--)
  46. {
  47. cout << *ai << ' '; // separate with a space
  48. }
  49. cout << endl << endl;
  50.  
  51. // assign num = 50 characters '!' to a string
  52. string hey;
  53. hey.assign(50,'!');
  54. cout << hey << endl;
  55.  
  56. // append hey to source
  57. source.append(hey);
  58. cout << source << endl << endl;
  59.  
  60. // add marker
  61. for(k = 0, n = 0; k < 18; k++, n++)
  62. {
  63. if (n > 9) n = 0;
  64. cout << n;
  65. }
  66. cout << endl;
  67. string text = "There was nobody at the Missing Persons' Bureau";
  68. cout << text << endl;
  69. // return the character at position 0 and 15 of text
  70. char ch = text.at(0);
  71. cout << "Character at position 0 = " << ch << endl;
  72. ch = text.at(15);
  73. cout << "Character at position 15 = " << ch << endl;
  74. // size() same as length()
  75. cout << "size = " << text.size() << " capacity = " << text.capacity();
  76. cout << endl << endl;
  77.  
  78. // copy num = 17 char of oldstr into newstr starting at index = 0
  79. string oldstr = "Frank is very fatherly!";
  80. // why not a little old stuff
  81. char newstr[80] = {0};
  82. oldstr.copy(newstr, 17, 0);
  83. cout << oldstr << endl;
  84. cout << newstr << endl << endl;
  85.  
  86. // add marker
  87. for(k = 0; k < 10; k++)
  88. {
  89. cout << k;
  90. }
  91. cout << endl;
  92. // removes num = 6 characters from the current string
  93. // starting at index = 23
  94. string sd("A really stupid person never becomes senile!");
  95. cout << sd << endl;
  96. sd.erase(23, 6 );
  97. cout << sd << endl << endl;
  98.  
  99. // return first occurrence of "stupid" within the current string
  100. // starting at index = 0, string::npos (-1) if nothing is found
  101. int pos = sd.find( "stupid", 0 );
  102. if ( pos != string::npos )
  103. {
  104. cout << "stupid is at index " << pos;
  105. }
  106. else
  107. {
  108. cout << "Didn't find stupid";
  109. }
  110. cout << endl;
  111.  
  112. // doing a reverse search from index, string::npos = fail (-1)
  113. pos = sd.rfind( "smart", 15 );
  114. cout << "smart is at index " << pos << " (fail=-1)" << endl << endl;
  115.  
  116. // inserts str2 into str1 at locations 11 and then 20
  117. string str1 = "Judges live on income";
  118. string str2 = " fixed";
  119. str1.insert(11, str2);
  120. str1.insert(20, str2);
  121. cout << str1 << endl;
  122. // add ! to the end
  123. str1.push_back('!');
  124. cout << str1 << endl << endl;
  125.  
  126. // replace characters of tooth string with num (here length)
  127. // characters from teeth string, beginning at index 21
  128. string tooth = "He ate it all with a toothpick";
  129. cout << tooth << endl;
  130. string teeth = "teethpick";
  131. tooth.replace( 21, teeth.length(), teeth );
  132. cout << tooth << endl << endl;
  133.  
  134. // return a substring of a string starting at index = 19
  135. string str("They will greet us with flowers!");
  136. string sub = str.substr(19);
  137. cout << "original string : " << str << endl;
  138. cout << "substring from 19 on : " << sub << endl;
  139. // change the size of string to num = 18 characters
  140. str.resize(18);
  141. cout << "resize to 18 char : " << str << endl;
  142. // dito, but pad excess (here past 18) with char '!'
  143. str.resize(21,'!');
  144. cout << "resize with ! padding: " << str << endl << endl;
  145.  
  146. // swap strings
  147. string first( "one, uno, eins, interesting lines" );
  148. string second( "two, dos, zwei, cake-flavored pie" );
  149. cout << "Before swap:" << endl;
  150. cout << "first = " << first << endl;
  151. cout << "second = " << second << endl << endl;
  152. first.swap( second );
  153. cout << "After swap:" << endl;
  154. cout << "first = " << first << endl;
  155. cout << "second = " << second << endl << endl;
  156.  
  157. // use at() and substr() to separate a string s1 into words
  158. cout << s1 << endl << endl; // show s1 in case you forgot
  159. int start = 0;
  160. // add a space to the end of s1 since this is our delimiter
  161. // this allows us to catch the last word properly
  162. s1.push_back(' ');
  163. for(k = 0; k < s1.size(); k++)
  164. {
  165. // found a space now show the word before that
  166. if (s1.at(k) == ' ')
  167. {
  168. // substr(startposition, length)
  169. cout << s1.substr(start, k - start) << endl;
  170. // update to new startposition
  171. start = k + 1;
  172. }
  173. }
  174. cout << endl;
  175.  
  176. cout << "Anyone can roast beef!" << endl;
  177.  
  178. cin.get(); // wait
  179. return 0;
  180. }
  181.  

All times are GMT -4. The time now is 8:46 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC