Skip to content

Instantly share code, notes, and snippets.

@DragonOsman
Created December 12, 2021 21:06
Show Gist options
  • Save DragonOsman/649ef30fe6d3798b400a9053513856fc to your computer and use it in GitHub Desktop.
Save DragonOsman/649ef30fe6d3798b400a9053513856fc to your computer and use it in GitHub Desktop.
// Osman Zakir
// 12 / 12 / 2021
// Beginning C++20: From Novice to Professional by Ivor Horton and Peter Van Weert
// Chapter 7 Exercise 8
// Exercise Specs:
/**
* A tautogram is a body of text in which all words start with the same letter. In
* English, an example would be “truly tautograms triumph, trumpeting trills to trounce terrible
* travesties.” Ask the user for a string, and verify whether it is a tautogram (ignoring casing). If
* it is, output also the letter by which each word starts.
*
* Note: True tautograms are truly tough to throw together, so perhaps you can bend the rules
* a bit? Maybe allow short words to glue the text together (such as “a,” “to,” “is,” “are,” etc.),
* or only require a certain percentage of words to begin with the same letter? have some fun
* with it!
*/
import <algorithm>;
import <iostream>;
import <string>;
import <vector>;
#include <cctype>
int main()
{
std::string text;
std::cout << "Enter a single sentence of text (you may add some words that don't begin with the same "
<< "letter if it seems hard to make a true tautogram):\n";
std::getline(std::cin, text);
constexpr double tautogram_percentage_value{ 75.0 / 100.0 };
const double tolerated_nontautogram_percent{ 15.0 / 100.0 };
const std::string separators{ " .,:;!\"" };
std::size_t start{ text.find_first_not_of(separators) };
std::vector<std::string> words;
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);
}
// take out any single quotes
using namespace std::string_literals;
for (auto& word : words)
{
if (word.starts_with('\'') && word.ends_with('\''))
{
word.erase(0, 1);
word.erase(word.length() - 1, 1);
}
if (word.starts_with('\''))
{
word.erase(0, 1);
}
if (word.ends_with('\''))
{
word.erase(word.length() - 1);
}
}
std::for_each(words.begin(), words.end(),
[](auto& word)
{
for (auto& ch : word)
{
ch = std::tolower(ch);
}
});
unsigned num_taut{}; // to hold number of letters starting with same letter
// (number of tautogram words)
const char first_letter{ words[0][0] };
for (const auto& word : words)
{
if (word.starts_with(std::tolower(first_letter)))
{
num_taut++;
}
}
if (num_taut == words.size())
{
std::cout << "Your sentence is a true tautogram!\nThe letter all words start with "
<< "is " << first_letter << ".\n";
}
else if (num_taut < words.size())
{
if (num_taut / words.size() >= tautogram_percentage_value)
{
std::cout << "Your sentence is at least 75% tautogram, so we'll say it is one.\n"
<< "The letter all words start with is " << first_letter << ".\n";
}
else if (num_taut / words.size() < tautogram_percentage_value)
{
std::cout << "Your sentence is not a tautogram.\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment