title: CS50 Caesar Cipher Bug
body: <p>Okay, so I am completely stumped. I cannot understand why this programs output acts as if there is a random key everytime.</p>
  <p>This program:</p>
  <pre><code>#include &lt;cs50.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;ctype.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main(int argc, string argv[])
  {
      string sKey = argv[1];
  // Make sure program was run with just one command-line argument
      if (argc != 2 || atoi(argv[1]) &lt; 0)
      {
          printf(&quot;Usage: ./caesar key\n&quot;);
          return 1;
      }
  
  //Counts length of string and checks if all chars are digits
      int counter = 0;
      for (int i = 0; i &lt; strlen(sKey); i++)
      {
          if isdigit(sKey[i])
          {
              counter++;
          }
      }
  
  //Checks if the key is a number
      if (counter != strlen(sKey))
      {
          printf(&quot;Usage: ./caesar key\n&quot;);
          return 1;
      }
  
  // Convert argv[1] from a `string` to an `int`
      int key = (int)sKey;
  
  // Prompt user for plaintext
      string plaintext = get_string(&quot;Plaintext:  &quot;);
      printf(&quot;Ciphertext: &quot;);
      for (int i = 0; i &lt; strlen(plaintext); i++)
      {
          if (isalpha(plaintext[i]) &amp;&amp; isupper(plaintext[i]))
          {
              printf(&quot;%c&quot;, (((plaintext[i] - 'A') + key) % 26) + 'A');
          }
          else if (isalpha(plaintext[i]) &amp;&amp; islower(plaintext[i]))
          {
              printf(&quot;%c&quot;, (((plaintext[i] - 'a') + key) % 26) + 'a');
          }
          else
          {
              printf(&quot;%c&quot;, plaintext[i]);
          }
      }
      printf(&quot;\n&quot;);
  }
  </code></pre>
  <p>Will output this:</p>
  <pre><code>caesar/ $ ./caesar 1
  Plaintext:  Hello, I'm Justin.
  Ciphertext: Fcjjm, G'k Hsqrgl.
  caesar/ $ ./caesar 1
  Plaintext:  Hello, I'm Justin.
  Ciphertext: Pmttw, Q'u Rcabqv.
  </code></pre>
  <p>It seems to be due to the modulo operator, because when I isolated it I could recreate the issue. Is it one of my included libraries? I solved the problem on my own and ended up looking up a solution on youtube only to find my solution performed the same operations as the correct solution. I must be missing something.</p>
  <p>Thank you</p>