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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool isMax(size_t w1, size_t w2) {
if(w1 > w2)
return true;
else
return false;
}
bool isEqual(size_t w1, size_t w2){
if(w1 == w2){
return true;
}
else return false;
}
int main(){
string s;
vector<string> word;
cout << "Enter a string: ";
getline(cin, s);
size_t found;
size_t found1;
found = s.find_first_not_of(" ");
found1 = s.find_first_of(" ");
while(found1 != string::npos){
word.push_back(s.substr(found,found1-found));
found = found1 + 1;
found1 = s.find_first_of(" ", found1 + 1);
}
found1 = string::npos;
int count = 0;
word.push_back(s.substr(found, found1));
unsigned int i = 0;
for (i = 0; i < word.size(); ++i ){
if (word[i].substr(word[i].length() - 1) == ".")
word[i] = word[i].substr(0, word[i].length() - 1);
if (word[i].substr(word[i].length() - 1) == ",")
word[i] = word[i].substr(0, word[i].length() - 1);
}
string sWord;
cout << "What word would you like to search for?: ";
cin >> sWord;
unsigned int max = 0;
for (unsigned int i = 0; i < word.size(); ++i){
if(word[i] == sWord)
++count;
}
i = 0;
vector<size_t> equals;
if (isEqual(word[i].length(), word[i+1].length())){
equals.push_back(i);
equals.push_back(i+1);
}
if (isMax(word[i].length(), word[i+1].length()))
max = i;
if (!isMax(word[i].length(), word[i+1].length()))
max = i+1;
i = 2;
do {
if (max == i) i += 1;
if (isEqual(word[max].length(), word[i].length())){
equals.push_back(max);
equals.push_back(i);
}
if ( !isMax(word[max].length(), word[i].length() ))
{
max = i;
}
i++;
} while (i < word.size() );
i = 0;
vector<string> maxW;
maxW.push_back(word[max]);
if (equals.size() != 1) {
for(i = 0; i < equals.size(); ++i){
if(word[max].length() == word[equals[i]].length()){
if (word[max] == word[equals[i]])
++i;
else
maxW.push_back(word[equals[i]]);
}
}
}
i = 0;
if ( maxW.size() != 1){
cout << "\nThe strings with the most characters are ";
while (i < maxW.size() ){
cout << maxW[i];
if (i != maxW.size() - 1)
cout << " & ";
else{
cout << ". ";
break;
}
i++;
}
}
else {
cout << "\nThe string with the most characters is " << maxW[maxW.size() - 1] << ".";
}
if (count == 1)
cout << "\nThere is " << count << " instance of the word " << sWord << "." << endl;
else
cout << "\nThere are " << count << " instances of the word " << sWord << "." << endl;
return 0;
}
|