How to generate all trigrams (3-character sequences) from a given word in C++

2 Answers

0 votes
#include <iostream>
#include <string>
#include <vector>

/*
    This program generates all *trigrams* of a given word.

    -------------------------------------------------------------------------
    What is a trigram?
    -------------------------------------------------------------------------
    A trigram is a sequence of exactly three consecutive characters taken
    from a word. It is created by sliding 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 often used in text analysis, search algorithms, and
    language modeling because they capture small, meaningful patterns
    within words.
*/

// Build and return all trigrams from a given word
std::vector<std::string> make_trigrams(const std::string& word) {
    std::vector<std::string> result;

    // If the word is shorter than 3 characters, no trigrams can be formed
    if (word.size() < 3) {
        return result;
    }

    // Slide a window of size 3 across the word
    // Using substr() is efficient and expressive for extracting slices
    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 using the helper function
    const auto trigrams = make_trigrams(input);

    std::cout << "\nTrigrams:\n";

    // Print each trigram
    for (const auto& t : trigrams) {
        std::cout << t << '\n';
    }
}


/*
run:

Enter a word: computer

Trigrams:
com
omp
mpu
put
ute
ter

*/

 



answered Sep 9 by avibootz
0 votes
#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

*/

 



answered Sep 9 by avibootz
...