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

2 Answers

0 votes
#include <stdio.h>
#include <string.h>

/*
    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 useful in text analysis, search algorithms, and language
    modeling because they capture small structural patterns inside words.
*/

/*
    Function: make_trigrams
    -----------------------
    Prints all trigrams of the given word.

    The function uses strlen() to determine the word length and then
    iterates with a sliding window of size 3. Each trigram is printed
    immediately, avoiding unnecessary storage.
*/
void make_trigrams(const char *word) {
    size_t len = strlen(word);

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

    // Slide a window of size 3 across the word
    for (size_t i = 0; i + 2 < len; ++i) {
        // Print the 3-character slice directly
        printf("%c%c%c\n", word[i], word[i + 1], word[i + 2]);
    }
}

int main(void) {
    char input[256] = "";

    printf("Enter a word: ");
    scanf("%255s", input);  // Read up to 255 chars safely

    printf("\nTrigrams:\n");
    make_trigrams(input);

    return 0;
}


/*
run:

Enter a word: computer

Trigrams:
com
omp
mpu
put
ute
ter


*/

 



answered Sep 9 by avibootz
edited Sep 9 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*
    -------------------------------------------------------------------------
    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 widely used in text processing, search algorithms,
    and language modeling because they capture small structural patterns
    inside words.
*/

/*
    Function: make_trigrams
    -----------------------
    Generates all trigrams from the given word and returns them as an array
    of dynamically allocated strings.

    Parameters:
        word       - input string
        count_out  - output parameter that receives the number of trigrams

    The caller is responsible for freeing the returned array and each
    trigram string inside it.
*/
char **make_trigrams(const char *word, size_t *count_out) {
    size_t len = strlen(word);

    // If the word is shorter than 3 characters, no trigrams can be formed
    if (len < 3) {
        *count_out = 0;
        return NULL;
    }

    // Number of trigrams is (len - 2)
    size_t count = len - 2;
    *count_out = count;

    // Allocate an array of char* pointers
    char **trigrams = malloc(count * sizeof(char*));
    if (!trigrams) {
        *count_out = 0;
        return NULL;
    }

    // Create each trigram
    for (size_t i = 0; i < count; ++i) {
        trigrams[i] = malloc(4);  // 3 chars + null terminator
        if (!trigrams[i]) {
            // Clean up previously allocated memory if allocation fails
            for (size_t j = 0; j < i; ++j) {
                free(trigrams[j]);
            }
            free(trigrams);
            *count_out = 0;
            return NULL;
        }

        // Copy the 3-character slice
        trigrams[i][0] = word[i];
        trigrams[i][1] = word[i + 1];
        trigrams[i][2] = word[i + 2];
        trigrams[i][3] = '\0';
    }

    return trigrams;
}

int main(void) {
    char input[256] = "";

    printf("Enter a word: ");
    scanf("%255s", input);

    size_t count = 0;

    // Generate trigrams
    char **trigrams = make_trigrams(input, &count);

    printf("\nTrigrams:\n");

    for (size_t i = 0; i < count; ++i) {
        printf("%s\n", trigrams[i]);
        free(trigrams[i]);  // Free each trigram string
    }

    free(trigrams);  // Free the array of pointers

    return 0;
}


/*
run:

Enter a word: computer

Trigrams:
com
omp
mpu
put
ute
ter

*/

 



answered Sep 9 by avibootz
...