Start by declaring an array storing your pattern (primes, in ascending order and then descending order, in such a way that reducing modulo the arrays size will cause a cycle). For example:
unsigned int prime[] = { 2, 3, 5, 7, 11, 13, 17, 13, 11, 7, 5, 3 };
Now, rather than adding (i + 1)
to each character in your loop, you'll want to add prime[i % (sizeof prime / sizeof *prime)]
. For example:
for (i = 0; s[i] != ''; i++)
{
putchar(s[i] + prime[i % (sizeof prime / sizeof *prime)]);
}
P.S. Technically, it's not required to be ASCII. Think of it this way: C and ASCII go together like coffee and cows milk. Most people love it, but occasionally someone will use coconut milk or cream instead.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…