#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
*/