#include <iostream>
#include <string>
#include <vector>
/*
-------------------------------------------------------------------------
What is a trigram?
-------------------------------------------------------------------------
A trigram is a sequence of exactly three consecutive characters taken
from a word. To generate all trigrams, we slide a window of length 3
across the string. Each step produces a new 3‑character slice.
Example:
Word: "magic"
Trigrams: "mag", "agi", "gic"
Trigrams are commonly used in text analysis, search algorithms,
and language modeling because they capture small structural patterns
inside words.
*/
/*
Function: make_trigrams
-----------------------
Returns a vector containing all trigrams of the given word.
The algorithm:
- If the word is shorter than 3 characters, return an empty vector.
- Otherwise, slide a window of size 3 across the word.
- Use substr() to extract each 3‑character sequence.
*/
std::vector<std::string> make_trigrams(const std::string& word) {
std::vector<std::string> result;
if (word.size() < 3) {
return result; // No trigrams possible
}
for (std::size_t i = 0; i + 2 < word.size(); ++i) {
result.push_back(word.substr(i, 3));
}
return result;
}
int main() {
std::string input;
std::cout << "Enter a word: ";
std::cin >> input;
// Generate trigrams
const auto trigrams = make_trigrams(input);
std::cout << "\nTrigrams:\n";
for (const auto& t : trigrams) {
std::cout << t << '\n';
}
}
/*
run:
Enter a word: computer
Trigrams:
com
omp
mpu
put
ute
ter
*/