If you wanted to clip a value "val" between 0 and MAX_VAL (included), you'd do something like
clipped_val = val % (MAX_VAL+1);
.
In your case you want to clip "val" between MIN_VAL and MAX_VAL.
If we define
val2 = val  MIN_VAL;
, then clipping "val" between MIN_VAL and MAX_VAL means clipping "val2" between 0 and (MAX_VALMIN_VAL), and we know how to do that:
clipped_val2 = val2 % (MAX_VALMIN_VAL+1);
Obtaining "clipped_val" is then straightforward :
clipped_val = clipped_val2 + MIN_VAL;
In your code, it would be something like:
ciphertext[inc] = MIN_ASCII_VALUE + ( (plaintext[inc] + key  MIN_ASCII_VALUE) % (MAX_ASCII_VALUE  MIN_ASCII_VALUE + 1) );
.
You should also beware of the behaviour of the "%" operator with negative numbers:
a%b
will give a result between (b1) and (b1), not between 0 and (b1).
If that's the latter you want, you must write your own function:
1 2 3 4 5 6 7

int PositiveModulus( int a, int b )
{
int mod = a%b;
if( mod < 0 )
mod += b;
return mod;
}
