fork download
  1. #include <algorithm>
  2. #include <cctype>
  3. #include <iomanip>
  4. #include <iostream>
  5. #include <map>
  6. #include <string>
  7. #include <sstream>
  8. #include <queue>
  9. #include <utility>
  10.  
  11.  
  12. inline bool isWordLetter(char c)
  13. {
  14. return std::isalnum(c) || c == '_';
  15. }
  16.  
  17.  
  18. inline void skipws(std::queue<char>& q)
  19. {
  20. while (!( q.empty() || isWordLetter(q.front()) ))
  21. q.pop();
  22. }
  23.  
  24.  
  25. /* Extracts valid word.
  26.  * Returns word or empty string if no word found till the end of queue
  27.  */
  28. std::string extractWord(std::queue<char>& q)
  29. {
  30. std::string word;
  31. bool again = false;
  32. do {
  33. again = false;
  34. skipws(q);
  35. /* Extract all consequent word character */
  36. while( !q.empty() && isWordLetter(q.front()) ) {
  37. word += q.front();
  38. q.pop();
  39. }
  40. /* Check if word extracted is actually word */
  41. if (word.size() < 2 || count_if(word.begin(), word.end(), isalpha) == 0) {
  42. word.clear();
  43. if ( !q.empty() )
  44. again = true;
  45. }
  46. } while (again); //Repeat until we find word or exhaust our queue
  47. std::transform(word.begin(), word.end(), word.begin(), tolower);
  48. return word;
  49. }
  50.  
  51.  
  52. std::string getEnvString()
  53. {
  54. extern char **environ; // needed to access your execution environment
  55. std::string envstr;
  56. int k = -1;
  57. while (environ[++k] != NULL) {
  58. envstr += environ[k];
  59. envstr += '\n';
  60. }
  61. return envstr;
  62. }
  63.  
  64.  
  65. std::pair<unsigned, std::string> wordEntries(const std::string& source, const std::string& word)
  66. {
  67. std::istringstream inp(source);
  68. unsigned word_count = 0;
  69. std::string output;
  70. std::string line;
  71. while(std::getline(inp, line, '\n')) {
  72. std::string::size_type pos;
  73. if ( (pos = line.find(word)) != std::string::npos ) {
  74. output += line;
  75. output += '\n';
  76. ++word_count;
  77. while ( (pos = line.find(word, pos + word.size())) != std::string::npos )
  78. ++word_count;
  79. }
  80. }
  81. return std::make_pair(word_count, output);
  82. }
  83.  
  84.  
  85. int main()
  86. {
  87. const std::string envstr = getEnvString();
  88. std::cout << envstr << std::endl;;
  89.  
  90. std::queue<char> line( {envstr.begin(), envstr.end()} );
  91. size_t wordCount = 0;
  92. std::map<std::string, unsigned> entries;
  93. std::string word;
  94. while((word = extractWord(line)) != "" ) {
  95. ++wordCount;
  96. ++entries[word];
  97. }
  98. std::cout << "There is " << wordCount << " words in envstring\n" <<
  99. "Unique words and their frequency:\n";
  100. for(auto& p: entries) {
  101. std::cout << std::setw(30) << p.first << ' ' << p.second << '\n';
  102. }
  103.  
  104. std::string envcpy = envstr;
  105. std::transform(envcpy.begin(), envcpy.end(), envcpy.begin(), tolower);
  106. std::cout << "Enter word to search for: ";
  107. std::cin >> word;
  108. std::transform(word.begin(), word.end(), word.begin(), tolower);
  109. auto result = wordEntries(envcpy, word);
  110. std::cout << "Word " << word << " is occured " << result.first << " times\n" <<
  111. "Lines where it appeared:\n" << result.second;
  112. }
  113.  
Success #stdin #stdout 0s 3488KB
stdin
home
stdout
TMPDIR=/tmp/x8W9oK
PATH=/usr/local/bin:/usr/bin:/bin
PWD=/home/I1wO8r
LANG=en_US.UTF-8
HOME=/home/I1wO8r
SHLVL=0

There is 20 words in envstring
Unique words and their frequency:
                           bin 3
                         en_us 1
                          home 3
                        i1wo8r 2
                          lang 1
                         local 1
                          path 1
                           pwd 1
                         shlvl 1
                           tmp 1
                        tmpdir 1
                           usr 2
                           utf 1
                        x8w9ok 1
Enter word to search for: Word home is occured 3 times
Lines where it appeared:
pwd=/home/i1wo8r
home=/home/i1wo8r