Find all words of length $k$ in lexicographic order
You can do it either inductively or recursively.
Inductively, you just write out the numbers from $0$ to $n^{k-1}$ in base $n$, adding $1$ to all digits.
Alternatively, you can simulate a counter in base $n$ – that would be faster.
Recursively, you can proceed as follows:

Generate all solutions for $k-1$, and output them with prefix 1.
Generate all solutions for $k-1$, and output them with prefix 2.
...
Generate all solutions for $k-1$, and output them with prefix $n$.

Alternatively:
– Generate all solutions for $k-1$.

For each solution $s$, output $s1, s2, \ldots, sn$.