1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
int main(){
#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
using namespace std;
const int ARRAY_SIZE = 1000;
const int NOT_FOUND = -1;
//Prototypes Start//
void loadData();
string convertCase(string word);
int search(string words[], string searchWord, int totalWords);
int findIndexofMost(int counts[], int totalWords);
//Prototypes End//
int main() {
loadData();
system("pause");
return 0;
}
void loadData() {
string filename, searchWord;
int numOfWords = 0, indexCount = 0, totalWords = 0, index = 0, most = 0;
int counts[ARRAY_SIZE] = { 0 };
string words[ARRAY_SIZE] = { "" };
ifstream inFile;
cout << "Enter a text file: ";
cin >> filename;
words[indexCount] = filename;
inFile.open(words[indexCount]);
while (inFile >> words[indexCount]) {
counts[numOfWords]++;
words[indexCount] = convertCase(words[indexCount]);
cout << words[indexCount];
if (inFile.get() == 32)
cout << " ";
else
cout << endl;
}
cout << endl << "total words: " << counts[numOfWords] << endl;
//------^^^^^^^^^^^^From here up is good^^^^^^^^^^^^^^^^^^^^^-----------
cout << "Search a word: ";
cin >> searchWord;
getline(inFile, searchWord);
inFile >> searchWord;
index = search(words, searchWord, totalWords);
counts[index]++;
if (index == NOT_FOUND) {
cout << "\"" << searchWord << "\"" << " was not found." << endl;
}
else {
cout << endl << "the word " << "\"" << searchWord << "\"" << " is found on at index " << index;
most = findIndexofMost(counts, totalWords);
cout << most;
}
}
string convertCase(string word) {
for (int i = 0; i < word.length(); i++) {
word[i] = tolower(word[i]);
}
return word;
}
int search(string words[], string searchWord, int totalWords) {
int index = NOT_FOUND;
for (int i = 0; i < totalWords; i++) {
if (words[i] == searchWord) {
index = i;
break;
}
}
return index;
}
int findIndexofMost(int counts[], int totalWords) {
int most;
most = counts[0];
for (int i = 0; i < totalWords; i++) {
if (counts[i] > most) {
most = counts[i];
}
}
return most;
}
return 0;
}
|