<mosaic.cnfolio.com>
PROGRAM
  1. /*
  2. Begin
  3.  
  4. scan"d" cipher_len
  5. if cipher length <2 or >16
  6. print "There must be 2 to 16 characters in cipher key."
  7. return -1
  8.  
  9. scan"s" cipher_key
  10. for i := 1 to cipher_len do
  11. if (cipher_key[i] >= 'a'and cipher_key[i] <= 'z')
  12.          if (cipher_type[cipher_key[i] - 'a'] == 0)
  13.             substitution_value[i] = cipher_key[(i + 1) % cipher_len];
  14.             cipher_type[cipher_key[i] - 'a'] = 1
  15. else
  16. print "Cipher cannot contain duplicates"
  17. return -1
  18. else
  19. print "Cipher should only contain lower case characters of alphabet(a-z)."
  20. return -1
  21.  
  22. scan"d" text_len
  23. if plain text length <4 or >256
  24. print "There must be 2 to 16 characters in cipher key."
  25. return -1
  26.  
  27. for i := 1 to cipher_len do
  28. if (not((plain_text[i] >= 'a'and plain_text[i] <= 'z') or (plain_text[i] == ' ')))
  29. print "Plain_text should only contain lower case characters of alphabet(a-z) and spaces."
  30. return -1
  31.  
  32. plain_text_changed used to mark if text has changed
  33. for i := 1 to cipher_len do
  34. print"Step d substitute c with c"
  35.  
  36. for j := 1 to text_len do
  37. if not plain_text_changed[j] and plain_text[j] == cipher_key[i])
  38.             plain_text[j] = substitution_value[i];
  39.             plain_text_changed[j] = 1;
  40. print "s"
  41.  
  42. end
  43.  
  44. */
  45. #include <stdio.h>
  46. #define MAX_CIPHER_KEY_LEN  16
  47. #define MAX_PLAINTEXT_LEN  256
  48.  
  49. int main()
  50. {
  51.    // input cipher key,corresponding substitution_value and input plain text
  52.    char cipher_key[MAX_CIPHER_KEY_LEN];
  53.    char substitution_value[MAX_CIPHER_KEY_LEN];
  54.    char plain_text[MAX_PLAINTEXT_LEN];
  55.  
  56.    // cipher length & plain text length
  57.    int cipher_len;
  58.    int text_len;
  59.    scanf("%d", &cipher_len);
  60.    // handle error cipher length
  61.    if (cipher_len < 2 || cipher_len>16) {
  62.       printf("There must be 2 to 16 characters in cipher key.\n");
  63.       return -1;
  64.    }
  65.    scanf("%s", cipher_key);
  66.    int cipher_type[26] = { 0 };
  67.    // check cipher type & duplicate
  68.    for (int i = 0; i < cipher_len; i++) {
  69.       if (cipher_key[i] >= 'a'&&cipher_key[i] <= 'z') {
  70.          if (cipher_type[cipher_key[i] - 'a'] == 0) {
  71.             substitution_value[i] = cipher_key[(i + 1) % cipher_len];
  72.             cipher_type[cipher_key[i] - 'a'] = 1;
  73.          }
  74.          else {
  75.             printf("Cipher cannot contain duplicates.\n");
  76.             return -1;
  77.          }
  78.       }
  79.       else {
  80.          printf("Cipher should only contain lower case characters of alphabet(a-z).\n");
  81.          return -1;
  82.       }
  83.    }
  84.    scanf("%d", &text_len);
  85.    // check plain text length
  86.    if (text_len < 4 || text_len>256) {
  87.       printf("There must be 4 to 256 characters in plain text.\n");
  88.       return -1;
  89.    }
  90.    // consume "\n"
  91.    getchar();
  92.    // check plain text type
  93.    fgets(plain_text, sizeof(plain_text), stdin);
  94.    for (int i = 0; i < text_len; i++) {
  95.       if (!((plain_text[i] >= 'a'&&plain_text[i] <= 'z') || (plain_text[i] == ' '))) {
  96.          printf("Plain_text should only contain lower case characters of alphabet(a-z) and spaces.\n");
  97.          return -1;
  98.       }
  99.    }
  100.    // plain_text_changed used to mark if text has changed
  101.    int plain_text_changed[MAX_PLAINTEXT_LEN] = { 0 };
  102.    // for each cipher
  103.    for (int i = 0; i < cipher_len; i++) {
  104.       printf("Step %d substitute %c with %c\n", i + 1, cipher_key[i], substitution_value[i]);
  105.       // for each char in plain text
  106.       for (int j = 0; j < text_len; j++) {
  107.          if (!plain_text_changed[j] && plain_text[j] == cipher_key[i]) {
  108.             plain_text[j] = substitution_value[i];
  109.             plain_text_changed[j] = 1;
  110.          }
  111.       }
  112.       printf("%s\n", plain_text);
  113.  
  114.    }
  115.    return 0;
  116. }