Skip to content

Lorem Ipsum Generator — C source

Generate placeholder Lorem Ipsum text - paragraphs and sentences with adjustable counts. Classic Latin word pool, instant copy.

This is the C implementation — the same logic the interactive tool runs, in a shareable, citable form.

/* lorem-ipsum — C port: canonical Lorem Ipsum generator (sentences, paragraphs, blocks). */
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/* The canonical word pool — identical across every port. Kept as one string
 * and indexed by scanning, so the pool never drifts from the TS reference. */
static const char POOL[] =
    "lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor "
    "incididunt ut labore et dolore magna aliqua enim ad minim veniam quis nostrud exercitation "
    "ullamco laboris nisi aliquip ex ea commodo consequat duis aute irure in reprehenderit "
    "voluptate velit esse cillum eu fugiat nulla pariatur excepteur sint occaecat cupidatat "
    "non proident sunt culpa qui officia deserunt mollit anim id est laborum";
#define WORD_COUNT 63

/* Uniform integer in [0, n); 0 for a non-positive bound, so a caller never
 * divides by zero. rand() is not cryptographic — fine for filler text. */
static int rand_below(int n) { return n <= 0 ? 0 : rand() % n; }

/* Pointer to the idx-th word inside POOL (single-space separated). */
static const char *word_at(int idx)
{
    const char *p = POOL;
    for (int i = 0; i < idx; i++) p = strchr(p, ' ') + 1;
    return p;
}

/* Append src to dst (prefixed by sep once dst is non-empty), truncating at cap. */
static void append(char *dst, size_t cap, char sep, const char *src)
{
    size_t len = strlen(dst);
    if (len + 1 >= cap) return;
    if (len) dst[len++] = sep;
    snprintf(dst + len, cap - len, "%s", src);
}

/* One capitalized sentence of min_words..max_words random words. */
static void sentence(char *out, size_t cap, int min_words, int max_words)
{
    int span = max_words - min_words + 1;
    if (span < 1) span = 1;    /* collapsed range = exactly min_words */
    int n = min_words + rand_below(span);
    out[0] = '\0';
    for (int i = 0; i < n; i++) {
        const char *w = word_at(rand_below(WORD_COUNT));
        char word[32];
        snprintf(word, sizeof word, "%.*s", (int)strcspn(w, " "), w);
        append(out, cap, ' ', word);
    }
    if (out[0]) out[0] = (char)toupper((unsigned char)out[0]); /* first char only */
    size_t len = strlen(out);
    if (len + 1 < cap) { out[len++] = '.'; out[len] = '\0'; }
}

/* sentences default-length (8..16 word) sentences joined by spaces. */
static void paragraph(char *out, size_t cap, int sentences)
{
    out[0] = '\0';
    for (int i = 0; i < sentences; i++) {
        char s[512];
        sentence(s, sizeof s, 8, 16);
        append(out, cap, ' ', s);
    }
}

int main(void)
{
    srand((unsigned)time(NULL));
    char para[2048];
    for (int p = 0; p < 3; p++) {   /* lorem(3, 5) — paragraphs joined by a blank line */
        if (p) putchar('\n');
        paragraph(para, sizeof para, 5);
        puts(para);
    }
}

Also available in 13 other languages

Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →