Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**** String and Char Remover By Ben Bollinger (C++) ****/
- //Includes:
- #include <iostream>
- #include <string>
- #include <vector>
- /**** Method Declarations ****/
- std::string removeString(std::string str, std::string rem);
- std::string removeChar(std::string str, char rem);
- std::vector<std::string> split(std::string str, char breakOn);
- int main() {
- //Declare Variables:
- std::string str = "";
- std::string remS = "";
- char remC = ' ';
- char charOrStr = ' ';
- //Ask For Word:
- std::cout << "Enter word: ";
- std::getline(std::cin, str);
- //Ask String or Char To Remove:
- std::cout << "\nEnter 'c' or 's' to remove a 'char' or 'string': ";
- std::cin >> charOrStr;
- //If Remove String:
- if (charOrStr == 's') {
- //Ask For String To Remove:
- std::cout << "\nEnter string to remove: ";
- std::cin >> remS;
- //Print:
- std::cout << removeString(str, remS) << "\n";
- }
- //If Remove Char:
- else if (charOrStr == 'c') {
- //Ask For Char To Remove:
- std::cout << "\nEnter char to remove: ";
- std::cin >> remC;
- //Print
- std::cout << removeChar(str, remC) << "\n";
- }
- //Pause When Done:
- system("pause");
- }
- /**** Method Implementations ****/
- //Remove String Method:
- std::string removeString(std::string str, std::string rem) {
- //Declare Variables:
- std::string newWord = "";
- int spaces = 0;
- //Add Space To Get Last Word:
- str += " ";
- //Count Spaces:
- for (int i = 0; i < str.length(); i++) {
- if (str.at(i) == ' ') {
- spaces++;
- }
- }
- //Build New Word:
- for (int i = 0; i < spaces; i++) {
- //If Word Iteration != rem:
- if (split(str, ' ')[i] != rem) {
- //Add It To newWord:
- newWord += split(str, ' ')[i] + " ";
- }
- }
- //Return newWord:
- return "\nNew Word: " + newWord + "\n\n'" + rem + "' has been removed\n";
- }
- //Remove Char Method:
- std::string removeChar(std::string str, char rem) {
- //Declare Variable:
- std::string newWord = "";
- //Traverse str:
- for (int i = 0; i < str.length(); i++) {
- //If Iteration != rem:
- if (str.at(i) != rem) {
- //Add It To newWord:
- newWord += str.at(i);
- }
- }
- //Return newWord:
- return "\nNew Word: " + newWord + "\n\nAll '" + rem + "'s have been removed\n";
- }
- //Split String Method:
- std::vector<std::string> split(std::string str, char breakOn) {
- //Declare Variables:
- std::vector<std::string> sVec;
- std::string statement = "";
- //Traverse str:
- for (unsigned int i = 0; i < str.size(); i++) {
- //If Iteration != breakOn:
- if (str.at(i) != breakOn) {
- //Add It To statement:
- statement += str.at(i);
- }
- //If Iteration == breakOn:
- else if (str.at(i) == breakOn) {
- //Push Vector With statement
- sVec.push_back(statement);
- //Reset Statement:
- statement = "";
- }
- }
- //Return sVec:
- return sVec;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement