title: Unexpected Outcome in Playfair Cipher Program
body: <p><strong>I'm having some trouble figuring out why I'm getting an unexpected outcome for the Digraphs in Row 4,5 and Column 4,5.</strong></p>
  
  <p>For Example - </p>
  
  <p>eg1) Plain Text Digraph  -> AR</p>
  
  <p>Expected Outcome  -> RM</p>
  
  <p>My outcome -> AM  </p>
  
  <p>eg2) Plain Text Digraph  -> AR</p>
  
  <p>Expected Outcome  -> RM</p>
  
  <p>My outcome -> AM</p>
  
  <p>Basically , if the Digraph has characters from Second Last and Last Rows or Columns Respectively , The first half of the Digraph wont change.</p>
  
  <pre><code>#include &lt;stdio.h&gt; 
  #include &lt;stdlib.h&gt; 
  #include &lt;string.h&gt; 
  
  int main(){
      char str[30],key[30];
  
      strcpy(key, "Monarchy"); 
      printf("\nKey text: %s", key); 
      printf("\nPlain Text (CAPS ONLY): ");
      gets(str);
  
      printf("\n Key Matrix : ");    
      char keyM[5][5]={'M','O','N','A','R','C','H','Y','B','D','E','F','G','I','K','L','P','Q','S','T','U','V','W','X','Z'};
  
      printf("\n - - - - -\n");
      for(int i=0;i&lt;5;i++)
      {
          for(int j=0;j&lt;5;j++)
          {
              printf("%c  ",keyM[i][j]);
          }
          printf("\n");
      }
      printf(" - - - - -");
  
      int pl = strlen(str);
      int CON=0;
      char pf[] = "Z";
      if(pl%2!=0)
      {
          strcat(str,pf);
          CON=1;
      }
  
      printf("\nAdjusted Text : %s \n - - - - - - \n",str);
  
      int lc=0;
      char Estr[30]="";
      for(int i=0;i&lt;pl+CON;i++)
      {
          char tmpD1;
          char tmpD2;
          int D1r,D1c,D2r,D2c;
  
          tmpD1=str[i];
          tmpD2=str[i+1];
          i++;
          lc++;
  
          printf("Plain Diagraphs : %c %c \n",tmpD1,tmpD2);
  
          for(int Row=0;Row&lt;5;Row++)
          {
              for(int Col=0;Col&lt;5;Col++)
              {
                  if(tmpD1==keyM[Row][Col])
                  {
                      D1r=Row;
                      D1c=Col;
                  }
                  if(tmpD2==keyM[Row][Col])
                  {
                      D2r=Row;
                      D2c=Col;
                  }
              }
          }
  
          printf("Diagraph locations : %d %d and %d %d \n",D1r,D1c,D2r,D2c);
  
          int ConEx=0;
          int ConEy=0;
          if(D1c==D2c)
          {
              if(D1r+1&gt;4)
              {
                  D1r=0;
                  ConEx=-1;
              }
              if(D2r+1&gt;4)
              {
                  D2r=0;
                  ConEx=-1;
              }
  
              printf("USES CASE 1 ");
              printf("Encrypted Diagraphs : %c %c \n ---- \n",keyM[D1r+1+ConEx][D1c],keyM[D2r+1+ConEx][D2c]);
              strncat(Estr,&amp;keyM[D1r+1+ConEx][D1c],1);
              strncat(Estr,&amp;keyM[D2r+1+ConEx][D2c],1);
  
          }
  
  
          else if(D1r==D2r)
          {
              if(D1c+1&gt;4)
              {
                  D1c=0;
                  ConEy=-1;
              }
              if(D2c+1&gt;4)
              {
                  D2c=0;
                  ConEy=-1;
              }
  
              printf("USES CASE 2 ");
              printf("Encrypted Diagraphs : %c %c \n ---- \n",keyM[D1r][D1c+1+ConEy],keyM[D2r][D2c+1+ConEy]);
              strncat(Estr,&amp;keyM[D1r][D1c+1+ConEy],1);
              strncat(Estr,&amp;keyM[D2r][D2c+1+ConEy],1);
          }
  
          else if((D1r!=D2r)&amp;&amp;(D1c!=D2c))
          {
              printf("USES CASE 3 ");
              printf("Encrypted Diagraphs : %c %c \n ---- \n",keyM[D1r][D2c],keyM[D2r][D1c]);
              strncat(Estr,&amp;keyM[D1r][D2c],1);
              strncat(Estr,&amp;keyM[D2r][D1c],1);
          }
  
      }
      printf("Loop Counter : %d",lc);
      printf("\nCypher String : %s",Estr);
  
  
      return 0;
  
  }
  </code></pre>