CBFalconer said:
rahul wrote:
... snip ...
It may, but I don't see any algorithm which can do the rotation
for an arbitrary character set. For argument's sake, there may
not be any pattern at all. 'a' may be 90, 'b' 91, then c 190....
and so on and on. Is it really possible to do this without
taking the particular char set into account?
Assuming the char set to be rotated and within is 'a ... z', then
you can do all sorts of things with:
char *theset = "abcdefghijklmnopqrstuvwxyz";
and use:
if (p = strchr(theset, ch)) /* ch is within the set */
ix = p - &theset;
ixnew = (ix + delta) % strlen(theset);
chnew = theset[ixnew];
}
else /* ignore the char */ ;
The sign of delta switches between encode and decode. There is no
portability problem, and the native char set can be anything you
want.
I see a possible performance problem here. For each letter, strchr()
scans, on average, half of the 26-character string (assuming a uniform
distribution). For each non-letter, it scans the entire string to
determine that it's not a letter. Something that should be O(N)
(where N is the number of characters being processed) becomes O(N*M),
where M is the number of letters in the alphabet.