code.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* Wurst is the best. Create a function that takes a string and
  2. replaces every mention of any type of sausage with the German
  3. word "Wurst," unless—of course—the sausage is already a type of
  4. German "Wurst" (i.e. "Bratwurst", see below),
  5. then leave the sausage name unchanged
  6. *
  7. * | German Wursts | Convert to Wurst |
  8. * | ------------------ | ------------------ |
  9. * | Bratwurst | Kielbasa |
  10. * | Kochwurst | Chorizo |
  11. * | Leberwurst | Moronga |
  12. * | Mettwurst | Salami |
  13. * | Rostbratwurst | Sausage |
  14. * | ~ | Andouille |
  15. * | ~ | Naem |
  16. * | ~ | Merguez |
  17. * | ~ | Gurka |
  18. * | ~ | Snorkers |
  19. * | ~ | Pepperoni |
  20. * Append sausages from the "Convert to Wurst" column with "wurst".
  21. * Do not replace any German sausage with the word "Wurst".
  22. * The word "Wurst" must be title case.
  23. */
  24. std::string wurstIsBetter(std::string str) {
  25. std::vector<std::string> sausagelist = {"Kielbasa", "Chorizo", "Moronga",
  26. "Salami", "Sausage", "Andouille", "Naem", "Merguez",
  27. "Gurka", "Snorkers", "Pepperoni"};
  28. // loops through my sausagelist and replaces each element with WURST
  29. for(int i=0;i<sausagelist.size();i++){
  30. size_t beginWord = str.find(sausagelist[i]);
  31. // if not found, search for lowercase
  32. if(beginWord==std::string::npos){
  33. std::string temp = sausagelist[i];
  34. temp[0] = tolower(sausagelist[i][0]);
  35. beginWord = str.find(temp);
  36. }
  37. // if sausagelist[i] was found, replace it with "Wurst".
  38. if(beginWord!=std::string::npos){
  39. str.replace(beginWord,sausagelist[i].size(),std::string("Wurst"));
  40. // go back and check again
  41. i--;
  42. }
  43. }
  44. /*std::string hasWurst ("Wurst");
  45. // check the string to see if WURST is in it;no change.
  46. if(str.find(hasWurst)!=std::string::npos){
  47. // no change.
  48. }
  49. // if wurst is not found, replace "str" with WURST.
  50. else {
  51. str=str.replace(0,str.size(),"Wurst");
  52. }
  53. */
  54. return str;
  55. }