<mosaic.cnfolio.com>
PROGRAM
  1. /*
  2. PSEUDOCODE
  3.  
  4. START
  5. SCAN input for ciphnum
  6. MAKE cipher key the length of ciphnum
  7. SCAN input for cipher key
  8. DISPLAY cipher key at output
  9. SCAN input for textnum
  10. MAKE plain text the length of textnum+1
  11. MAKE encrypted text the length of textnum+1
  12. SCAN input for plain text
  13. DISPLAY plain text at output
  14. IF letter to be changed is not the last character of cipher key
  15.    SUBSTITUTE letter with next character of cipher key
  16.    DISPLAY substitution step at output
  17. ELSE
  18.    SUBSTITUTE letter with the first character of cipher key
  19.    DISPLAY substitution step at output
  20. IF all letters have been considered
  21.    DISPLAY encrypted text at output
  22. END
  23.  
  24. */
  25.  
  26.  
  27.  
  28. #include <stdio.h>
  29. int ciphnum; // ciphnum = length of cipher key
  30. int textnum; // textnum = length of plain text
  31.  
  32.  
  33. int main( void )
  34. {
  35. scanf("%d", &ciphnum); // read input for ciphnum
  36. char ciphkey[ciphnum]; // ciphkey is length of ciphnum
  37. for (int i = 0; i <= ciphnum;i++) //move to next step
  38. {
  39. scanf("%c",&ciphkey[i]); //read input for ciphkey
  40. printf("%c",ciphkey[i]); //show ciphkey at output
  41. }
  42. scanf("%d",&textnum); //read input for textnum
  43. char tplain[textnum +1]; //make tplain length of textnum +1
  44. char enct[textnum +1]; //make enct length of textnum +1
  45. for (int i = 0; i <= textnum; i++) //move to next step   
  46. {
  47.    scanf("%c", &tplain[i]); //read input for tplain
  48.    printf("%c",tplain[i]); // show tplain at output
  49. }
  50.  printf("\n");
  51. for (int i = 1; i <= textnum; i++) //move to next step      
  52. {
  53.    enct[i] = tplain[i]//tplain and enct must be same length                  
  54. }
  55. int letterp; //letterp = letter to be changed
  56. int letterc; //letter to be changed to
  57. int sub; //substituted
  58. for (int i = 1; i <= textnum; i++)     
  59. {
  60.    for (int j = 1; j <= ciphnum; j++)     
  61.    { 
  62.   letterp= tplain[i] - '0';
  63.   letterc= ciphkey[j] - '0';
  64.          
  65.          if ( letterp == letterc && j != ciphnum && sub == 0)
  66.       {
  67.             enct[i] = ciphkey[j+1]; //substitute letter
  68.          printf("NEXT change %c to %c \n",ciphkey[j],ciphkey[j+1]); //show subsitution made at output
  69.          sub = 1; //repeat
  70.       }
  71.      
  72.        else if (letterp == letterc && j == ciphnum && sub == 0)
  73.       {
  74.          enct[i] = ciphkey[1]; //loop
  75.          printf("NEXT change %c to %c \n",ciphkey[j],ciphkey[1]); //show substitution made at output
  76.          sub = 1; //repeat
  77.     }     
  78.        
  79.       sub = 0; //substitutions made
  80.  }     
  81. }
  82. for (int i =1; i<=textnum; i++) //when all above complete
  83. {
  84. printf("%c",enct[i]); //print encrypted text
  85.  
  86. }
  87.  
  88. }