Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <algorithm>
- #include <string>
- using namespace std;
- char takeSource() {
- char choice;
- cin >> choice;
- while ((choice != '1') && (choice != '2')) {
- while (cin.get() != '\n') {
- cin.clear();
- }
- cout << "Incorrect input!!!\nSelect the source:\n1:Console\n2:File\nEnter 1 or 2: ";
- cin >> choice;
- }
- return choice;
- }
- string takeInPath() {
- string path;
- bool isIncorrect;
- cout << "Enter file path: ";
- do {
- isIncorrect = true;
- cin >> path;
- ifstream fin(path);
- if (fin.is_open()) {
- isIncorrect = false;
- fin.close();
- }
- else {
- cout << "File is not found\nEnter file path: ";
- }
- if (!isIncorrect && ((path.length() < 4) || (!(path.substr(path.length() - 4) == ".txt")))) {
- cout << "File is found, but it is not \".txt\" type file\nEnter file path : ";
- isIncorrect = true;
- }
- } while (isIncorrect);
- return path;
- }
- string takeOutPath() {
- string path;
- bool isIncorrect;
- cout << "Enter file path: ";
- do{
- isIncorrect = false;
- cin >> path;
- if ((path.length() < 4) || (!(path.substr(path.length() - 4) == ".txt"))) {
- cout << "It should be a \".txt\" file\nEnter file path: ";
- isIncorrect = true;
- }
- } while (isIncorrect);
- return path;
- }
- string takeSentenceFromFile(const string path) {
- ifstream fin(path);
- string sentence;
- getline(fin, sentence);
- if (!fin.eof()) {
- sentence = "";
- }
- fin.close();
- return sentence;
- }
- string takeSentenceFromConsole() {
- string sentence;
- string temp;
- cout << "Enter the sentence: ";
- getline(cin, temp);
- getline(cin, sentence);
- return sentence;
- }
- string takeSentence (const char source) {
- string inPath;
- string sentence;
- if (source == '1') {
- sentence = takeSentenceFromConsole();
- }
- else {
- inPath = takeInPath();
- sentence = takeSentenceFromFile(inPath);
- if (sentence == "") {
- inPath = takeInPath();
- sentence = takeSentenceFromFile(inPath);
- }
- }
- return sentence;
- }
- string replaceAllToSpace(string sentence, char symbol) {
- int index = sentence.find(symbol);
- while (index != -1) {
- sentence.replace(index, 1, " ");
- index = sentence.find(symbol);
- }
- return sentence;
- }
- string makeSentenceNormal(string sentence) {
- sentence = replaceAllToSpace(sentence, '.');
- sentence = replaceAllToSpace(sentence, ',');
- sentence = replaceAllToSpace(sentence, '-');
- sentence = replaceAllToSpace(sentence, ';');
- sentence = replaceAllToSpace(sentence, '!');
- sentence = replaceAllToSpace(sentence, '?');
- sentence = replaceAllToSpace(sentence, ':');
- sentence = replaceAllToSpace(sentence, '—');
- return sentence;
- }
- vector <string> findArrayOfWords(string sentence) {
- sentence = makeSentenceNormal(sentence);
- vector <string> arrayOfWords(100);
- int wordsCounter = 0;
- string word;
- int index = sentence.find(" ");
- while (index != -1) {
- word = sentence.substr(0, index);
- if (word != "") {
- arrayOfWords[wordsCounter] = word;
- wordsCounter++;
- }
- sentence = sentence.substr(index + 1);
- index = sentence.find(" ");
- }
- if (sentence != "") {
- arrayOfWords[wordsCounter] = sentence;
- wordsCounter++;
- }
- vector <string> totalArrayOfWords(wordsCounter);
- for (int i = 0; i < wordsCounter; i++) {
- totalArrayOfWords[i] = arrayOfWords[i];
- }
- return totalArrayOfWords;
- }
- bool areWordsSame(string word, string lastWord){
- transform(word.begin(), word.end(), word.begin(), tolower);
- transform(lastWord.begin(), lastWord.end(), lastWord.begin(), tolower);
- if (word == lastWord) {
- return true;
- }
- else {
- return false;
- }
- }
- int findAnswer(vector <string> array) {
- int counter = 0;
- for (int i = 0; i < array.size(); i++) {
- if (areWordsSame(array[i], array[array.size() - 1])){
- counter++;
- }
- }
- return counter;
- }
- void outputToFile(const string path, const string sentence, const int answer) {
- ofstream fout;
- fout.open(path);
- fout << "Sentence is:\n" << sentence << "\nLast word founded " << answer << " times.";
- fout.close();
- cout << "Done";
- }
- void output(const char source, const string sentence, const int answer) {
- string outPath;
- if (source == '1') {
- cout << "Sentence is:\n" << sentence << "\nLast word founded " << answer << " times.";
- }
- else {
- outPath = takeOutPath();
- outputToFile(outPath, sentence, answer);
- }
- }
- int main(){
- cout << "Welcome to the program that count how many times the last word occurs in a sentence.\nSelect the source for entering the sentence:\n1:Console\n2:File\nEnter 1 or 2: ";
- char inputSource = takeSource();
- string sentence = takeSentence(inputSource);
- vector <string> arrayOfWords = findArrayOfWords(sentence);
- int answer = findAnswer(arrayOfWords);
- cout << "Select the source for output:\n1:Console\n2:File\nEnter 1 or 2: ";
- char outputSource = takeSource();
- output(outputSource, sentence, answer);
- }
Add Comment
Please, Sign In to add comment