You probably want to make all text upper or lower case (because it gives less hints to a decryptor - not that this sort of thing is particularly hard to crack on statistical basis anyways, but you wouldn't want someone to know where sentences start right?).
Also, that makes it quite easy to do the whole thing in one step[1]:
Code:
c = tolower(getchar());
if (isalpha(c))
d = (((c -'a') + 6) % 26) + 'a';
putchar(d);
count++;
if (count % 5 == 0) putchar(" ");
}
This replaces the TWO if-statements by using the fact that (n + 26) % 26 == n [where n is smaller than 26].
[1] Assuming that the input is English and ASCII, we can use this method to maintain the original case - it's only a little bit more complex:
Code:
c = getchar();
if (isalpha(c))
int t = c & 32; // if this is 0, it's upper case, if it's 32 it's lower case.
c &= ~32; // makes it upper case temporarily.
d = (((c -'A') + 6) % 26) + 'A' + t; // t adds in "lowercase" if it was LC to start with.
putchar(d);
count++;
if (count % 5 == 0) putchar(" ");
}
--
Mats