Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 months ago
3.9 kB
2
Indexable
Never
#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 = "more") {
	int counting = 0;
	size_t position = userText.find(userPhrase);
	while (position != std::string::npos) {
		counting++;
		position = userText.find(userPhrase, position + 1);
	}
	return counting;
}

int GetNumOfNonWSCharacters(const string userText) {
	int counting = 0;

	for (char c : userText) {
		if (c == ' ') {

		}
		else {
			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;
}
Leave a Comment