Skip to content

Instantly share code, notes, and snippets.

@DragonOsman
Last active December 8, 2021 21:01
Show Gist options
  • Save DragonOsman/8c9483ec3bac4d31917c1b5d98b6bdf0 to your computer and use it in GitHub Desktop.
Save DragonOsman/8c9483ec3bac4d31917c1b5d98b6bdf0 to your computer and use it in GitHub Desktop.
// Osman Zakir
// 12 / 7 / 2021
// Beginning C++20: From Novice to Professional by Ivor Horton and Peter Van Weert
// Chapter 7 Exercise 2
// Exercise Specs:
/**
* Write a program that reads text entered over an arbitrary number of lines. Find
* and record each unique word that appears in the text and record the number of occurrences
* of each word. Output the words and their occurrence counts. Words and counts should align
* in columns. The words should align to the left; the counts to the right. There should be three
* words per row in your table.
*/
import <iostream>;
import <vector>;
import <format>;
import <string>;
#include <boost/algorithm/string/predicate.hpp>
int main()
{
std::string text;
std::cout << "Enter some text terminated by *:\n";
std::getline(std::cin, text, '*');
const std::string separators{ " ,;:.\"!?'\n" };
std::size_t start{ text.find_first_not_of(separators) };// Osman Zakir
// 12 / 7 / 2021
// Beginning C++20: From Novice to Professional by Ivor Horton and Peter Van Weert
// Chapter 7 Exercise 2
// Exercise Specs:
/**
* Write a program that reads text entered over an arbitrary number of lines. Find
* and record each unique word that appears in the text and record the number of occurrences
* of each word. Output the words and their occurrence counts. Words and counts should align
* in columns. The words should align to the left; the counts to the right. There should be three
* words per row in your table.
*/
import <iostream>;
import <vector>;
import <format>;
import <string>;
#include <boost/algorithm/string/predicate.hpp>
int main()
{
std::string text;
std::cout << "Enter some text terminated by *:\n";
std::getline(std::cin, text, '*');
const std::string separators{ " ,;:.\"!?'\n" };
std::size_t start{ text.find_first_not_of(separators) };
std::vector<std::string> words;
// loop to find all words and put them in words vector
while (start != std::string::npos)
{
std::size_t end{ text.find_first_of(separators, start + 1) };
if (end == std::string::npos)
{
end = text.length();
}
words.push_back(text.substr(start, end - start));
start = text.find_first_not_of(separators, end + 1);
}
std::vector<std::string> unique_words;
std::vector<int> word_counts;
unique_words.push_back(words[0]);
word_counts.push_back(1);
bool found{ false };
for (std::size_t i{ 1 }; i < words.size(); ++i)
{
for (std::size_t j{}; !found && j < unique_words.size(); ++j)
{
if (boost::iequals(unique_words[j], words[i]))
{
++word_counts[j];
found = true;
}
}
if (!found)
{
unique_words.push_back(words[i]);
word_counts.push_back(1);
}
}
std::size_t max_length{};
for (const auto& word : words)
{
if (max_length < word.length())
{
max_length = word.length();
}
}
std::cout << std::endl;
for (const auto& unique_word : unique_words)
{
for (const auto& word_count : word_counts)
{
std::cout << std::format("{:{}} {:5}", unique_word, max_length + 2, word_count);
}
std::cout << std::endl;
}
std::cout << std::endl;
}
std::vector<std::string> words;
// loop to find all words and put them in words vector
while (start != std::string::npos)
{
std::size_t end{ text.find_first_of(separators, start + 1) };
if (end == std::string::npos)
{
end = text.length();
}
words.push_back(text.substr(start, end - start));
start = text.find_first_not_of(separators, end + 1);
}
std::vector<std::string> unique_words;
std::vector<int> word_counts;
std::size_t count_index{};
unique_words.push_back(words[0]);
word_counts.push_back(1);
for (std::size_t i{ 1 }; i < words.size(); ++i)
{
for (const auto& unique_word : unique_words)
{
if (boost::iequals(unique_word, words[i]))
{
++word_counts[count_index];
}
else
{
unique_words.push_back(words[i]);
word_counts.push_back(1);
++count_index;
}
}
}
std::size_t max_length{};
for (const auto& word : words)
{
if (max_length < word.length())
{
max_length = word.length();
}
}
std::cout << std::endl;
for (const auto& unique_word : unique_words)
{
for (const auto& word_count : word_counts)
{
std::cout << std::format("{:{}} {:5}", unique_word, max_length + 2, word_count);
}
std::cout << std::endl;
}
std::cout << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment