Advertisement
gamesrsly

Find The Biggest String(word) In Sentence(with noise)

Oct 7th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <sstream>
  5. #include <cstring>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. int main(){
  11.     string str = "";
  12.     std::vector<string> sentence;
  13.     string temp = "";
  14.     string maxString = "";
  15.     int resultCompare = 0;
  16.    
  17.     // forbidden symbols
  18.     char characters[] = "0123456789";
  19.    
  20.     // word of the sentence(vector of characters in a word)
  21.     string word = "";
  22.    
  23.     // create a stringstream
  24.     stringstream ss;
  25.    
  26.     // get the sentence string
  27.     getline(cin, str);
  28.    
  29.     // populate the stringstream
  30.     ss << str;
  31.    
  32.     // get each vector(word) in the vector of strings(sentence) from the stringstream populated with the sentence
  33.     while(ss >> word) {
  34.         // using #include <cstring> for strlen
  35.         // erase every character that is not allowed (from the forbidden symbols array) from the word(vector of characters)
  36.         // using the erase-remove idiom
  37.         for(size_t j = 0; j < strlen(characters); j++) {
  38.             word.erase(std::remove(word.begin(), word.end(), characters[j]), word.end());
  39.         }
  40.         //push the vector of characters to the sentence vector of strings
  41.         sentence.push_back(word);
  42.     }
  43.    
  44.     // loop through the vector of strings(sentence)
  45.     for(int i = 0; i < sentence.size(); i++) {
  46.         // set each word to temp string
  47.         temp = sentence[i];
  48.         // compare temp an maxString by size of the strings
  49.         if(temp.size() >= maxString.size()) {
  50.             // compare tema and maxString lexicographically (using #include <algorithm>
  51.             resultCompare = lexicographical_compare(maxString.begin(), maxString.end(), temp.begin(), temp.end());
  52.             // if result is 0 -> first string is equal to second string lexicographically
  53.             // if result is 1 -> first string is bigger to second string lexicographically
  54.             // if result is negative -> first string is smaller than second string lexicographically
  55.             if(resultCompare == 0 || resultCompare == 1) {
  56.                 // if strings equal or first string is bigger lexicographically, set maxString to temp(found the biggest string)
  57.                 maxString = temp;
  58.             }
  59.         }
  60.     }
  61.    
  62.     // if vector of allowed chars(word without numbers) was found in the sentence
  63.     // and the it is the biggest after size and lexicographical checks, output the biggest word
  64.     if(!maxString.empty()) {
  65.         cout << maxString << endl;
  66.         // if not output "no noise"
  67.     } else {
  68.         cout << "no noise" << endl;
  69.     }
  70.    
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement