/********************************************************************
 * TMA02 q3c.cpp
 * The decrypt() function accepts a decrypted string.
 * It decrypts the string by left-shifting the lexicographical order
 * of each capital letter by a fixed number - key.
 * Programming by Allen Lam (99043953)
 * Dec 2000
 ********************************************************************/
#include <string.h>

/* determine whether the input char is a capital letter */
int isCapital(char a){
  if ((a>=65)&&(a<=90)) return 1;
  else return 0;
}

/* Left-shift the lexicographical order of char *a */
void LShift(char* a, int key){
  if (*a-key>=65) *a = *a-key;
  else *a = *a+26-(key%26);
}

void decrypt(char* message, int length, int key){
  int i;
  /* for safety reason int length is checked against the actual length */
  if (strlen(message)<(size_t)length) length = strlen(message);
  for (i=0; i<length; i++)
    if (isCapital(*(message+i))) LShift((message+i), key);
}

back