Advertisement
TermSpar

Remove Char or String

Nov 19th, 2016
836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. /**** String and Char Remover By Ben Bollinger (C++) ****/
  2.  
  3. //Includes:
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7.  
  8. /**** Method Declarations ****/
  9. std::string removeString(std::string str, std::string rem);
  10. std::string removeChar(std::string str, char rem);
  11. std::vector<std::string> split(std::string str, char breakOn);
  12.  
  13. int main() {
  14.  
  15.     //Declare Variables:
  16.     std::string str = "";
  17.     std::string remS = "";
  18.     char remC = ' ';
  19.     char charOrStr = ' ';
  20.  
  21.     //Ask For Word:
  22.     std::cout << "Enter word: ";
  23.     std::getline(std::cin, str);
  24.  
  25.     //Ask String or Char To Remove:
  26.     std::cout << "\nEnter 'c' or 's' to remove a 'char' or 'string': ";
  27.     std::cin >> charOrStr;
  28.  
  29.     //If Remove String:
  30.     if (charOrStr == 's') {
  31.         //Ask For String To Remove:
  32.         std::cout << "\nEnter string to remove: ";
  33.         std::cin >> remS;
  34.  
  35.         //Print:
  36.         std::cout << removeString(str, remS) << "\n";
  37.     }
  38.     //If Remove Char:
  39.     else if (charOrStr == 'c') {
  40.         //Ask For Char To Remove:
  41.         std::cout << "\nEnter char to remove: ";
  42.         std::cin >> remC;
  43.  
  44.         //Print
  45.         std::cout << removeChar(str, remC) << "\n";
  46.     }
  47.  
  48.     //Pause When Done:
  49.     system("pause");
  50. }
  51.  
  52. /**** Method Implementations ****/
  53.  
  54. //Remove String Method:
  55. std::string removeString(std::string str, std::string rem) {
  56.     //Declare Variables:
  57.     std::string newWord = "";
  58.     int spaces = 0;
  59.  
  60.     //Add Space To Get Last Word:
  61.     str += " ";
  62.  
  63.     //Count Spaces:
  64.     for (int i = 0; i < str.length(); i++) {
  65.         if (str.at(i) == ' ') {
  66.             spaces++;
  67.         }
  68.     }
  69.     //Build New Word:
  70.     for (int i = 0; i < spaces; i++) {
  71.         //If Word Iteration != rem:
  72.         if (split(str, ' ')[i] != rem) {
  73.             //Add It To newWord:
  74.             newWord += split(str, ' ')[i] + " ";
  75.         }
  76.     }
  77.     //Return newWord:
  78.     return "\nNew Word: " + newWord + "\n\n'" + rem + "' has been removed\n";
  79. }
  80.  
  81. //Remove Char Method:
  82. std::string removeChar(std::string str, char rem) {
  83.     //Declare Variable:
  84.     std::string newWord = "";
  85.  
  86.     //Traverse str:
  87.     for (int i = 0; i < str.length(); i++) {
  88.         //If Iteration != rem:
  89.         if (str.at(i) != rem) {
  90.             //Add It To newWord:
  91.             newWord += str.at(i);
  92.         }
  93.     }
  94.     //Return newWord:
  95.     return "\nNew Word: " + newWord + "\n\nAll '" + rem + "'s have been removed\n";
  96. }
  97.  
  98.  
  99. //Split String Method:
  100. std::vector<std::string> split(std::string str, char breakOn) {
  101.     //Declare Variables:
  102.     std::vector<std::string> sVec;
  103.     std::string statement = "";
  104.  
  105.     //Traverse str:
  106.     for (unsigned int i = 0; i < str.size(); i++) {
  107.         //If Iteration != breakOn:
  108.         if (str.at(i) != breakOn) {
  109.             //Add It To statement:
  110.             statement += str.at(i);
  111.         }
  112.         //If Iteration == breakOn:
  113.         else if (str.at(i) == breakOn) {
  114.             //Push Vector With statement
  115.             sVec.push_back(statement);
  116.             //Reset Statement:
  117.             statement = "";
  118.         }
  119.     }
  120.     //Return sVec:
  121.     return sVec;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement