Untitled
unknown
c_cpp
a year ago
5.0 kB
6
Indexable
#include <bitset> #include <iostream> #include <iomanip> #include <string> #include <cctype> #include <vector> #include <algorithm> using namespace std; string ShortenSpace(string userText) { size_t position; while ((position = userText.find(" ")) != string::npos) { userText.replace(position, 2, " "); } return userText; } int FindText(string userText, string userPhrase = "hey") { //let's first get the words again. int startingPosition = 0; size_t findingSpaces = userText.find(' '); //creating a vector to hold my words to compare vector<string> userWord(0); while (findingSpaces != string::npos) { string word = userText.substr(startingPosition, findingSpaces - startingPosition); startingPosition = findingSpaces + 1; //find the next space findingSpaces = userText.find(' ', startingPosition); for (int i = 0; i < word.size(); ++i) { if (ispunct(word[i])) { word.erase(i--, 1); } } userWord.push_back(word); } //getting the last word if (findingSpaces == string::npos) { string lastWord = userText.substr(startingPosition); //going over the last word, and removing the puncuation at the end to make sure we count it just in case the last word is part of userPhrase for (int i = 0; i < lastWord.size(); ++i) { if (ispunct(lastWord[i])) { lastWord.erase(i--, 1); } } userWord.push_back(lastWord); } int countingInstance = 0; for (int i = 0; i < userWord.size(); ++i) { string phrase; for (size_t j = i; j < userWord.size() && j - i + 1 <= userPhrase.size(); ++j) { if (!phrase.empty()) phrase += " "; phrase += userWord[j]; if (phrase == userPhrase) ++countingInstance; } } return countingInstance; } int GetNumOfNonWSCharacters(const string userText) { int counting = 0; for (char c : userText) { if (c != ' ') { counting++; } } return counting; } string ReplaceExclamation(string userText) { string newUserText = userText; for (char& c : newUserText) { userText = c; if (c == '!') { c = '.'; } } return newUserText; } int GetNumOfWords(const string userText) { int startingPosition = 0; int countingWords = 0; size_t findingSpace = userText.find(' '); while (findingSpace != string::npos) { string wordFromUserText = userText.substr(startingPosition, findingSpace - startingPosition); if (!wordFromUserText.empty()) { countingWords++; } startingPosition = findingSpace + 1; findingSpace = userText.find(' ', startingPosition); } string lastWordInUserText = userText.substr(startingPosition); if (!lastWordInUserText.empty()) { countingWords++; } return countingWords; } void ExecuteMenu(const char userChoice, const string userText) { //Number of non-whitespace character if (userChoice == 'c') { int numOfNonWSChar = GetNumOfNonWSCharacters(userText); cout << "Number of non-whitespace characters: " << numOfNonWSChar << endl; cout << endl; } //Number of words else if (userChoice == 'w') { int wordsCounted = GetNumOfWords(userText); cout << "Number of words: " << wordsCounted << endl; cout << endl; } //Find a specific text (word or phrase) else if (userChoice == 'f') { string userPhrase; cin.ignore(); cout << "Enter a word or phrase to be found:"; getline(cin, userPhrase); int instances = FindText(userText, userPhrase); cout << endl; cout << "\"" << userPhrase << "\"" << " instances: " << instances << endl; cout << endl; } //Replace all !'s else if (userChoice == 'r') { string newUserText = ReplaceExclamation(userText); cout << "Edited text: " << newUserText << endl; cout << endl; } //Shortern spaces else if (userChoice == 's') { string spacesShortened = ShortenSpace(userText); cout << "Edited text: " << spacesShortened << endl; cout << endl; } } void PrintMenu() { cout << "MENU" << endl; cout << "c - Number of non-whitespace characters" << endl; cout << "w - Number of words" << endl; cout << "f - Find text" << endl; cout << "r - Replace all !'s" << endl; cout << "s - Shorten spaces" << endl; cout << "q - Quit" << endl; } int main() { string userText; char userChoice; cout << "Enter a sample text:" << endl; getline(cin, userText); cout << endl; cout << "You entered: " << userText << endl; cout << endl; while (true) { PrintMenu(); cout << endl; cout << "Choose an option:" << endl; cin >> userChoice; char choice = tolower(userChoice); if ((choice == 'c') || (choice == 'w') || (choice == 'f') || (choice == 'r') || (choice == 's') || (choice == 's')) { ExecuteMenu(userChoice, userText); } else if (choice == 'q') { //cout << "You decided to QUIT." << endl; //cout << "Have a good day! :)"; break; } else { cout << "You didn't enter the correct ouput option. Try again." << endl; } } return 0; }
Editor is loading...
Leave a Comment