title: Spaces in caesar cipher in C
body: <p>This code must encrypt all lowercase letters and show error messages if the input is anything but lowercase. If <code>Key</code> is zero, then the input will be reversed.</p>
  
  <p>It works good, but when it comes to spaces, things are going wrong.</p>
  
  <p>What must appear:</p>
  
  <pre><code>plain text: notverysecure thiscaeserwas
  
  encrypted text: jkpranuoayqna pdeoywaoanswo
  </code></pre>
  
  <p>What appears:</p>
  
  <pre><code>plain text: notverysecure thiscaeserwas
  
  encrypted text: abgirelfrpher guvfpnrfrejnf
  </code></pre>
  
  <p>However if the input is: </p>
  
  <pre><code>plain text: notverysecurethiscaeserwas
  
  encrypted text: jkpranuoayqnapdeoywaoanswo
  </code></pre>
  
  <p>My guess is that the space is involved into calculation of <code>Key</code>, but my attempts to fix this are doing nothing. </p>
  
  <p>My question is how to make the code to read spaces in the input, but to not involve them into any kind of manipulation</p>
  
  <p>The code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;ctype.h&gt;
  //*******************************************************************
  // Function to reverse the input string
  void rev (const char *const c)
  {
  if (*c)
  {
  
    rev (c + 1);
    putchar (*c);
  
   }
  }
  //*******************************************************************
  
  int main (void)
  {
  char string[350];
  int Key;
  
  printf ("plain text: ");
  fgets (string, sizeof (string), stdin);
  Key = 256 % (strlen (string) - 1);
  //***************************************************************
  //ERROR CHECK
  
  if (strlen (string) &gt; 256)
   {
    printf ("[ERR] too many characters\n");
    return -1;
  }
  
  
  for (int counter = 0; string[counter] != '\n'; counter++)
  {
  
    if ( !(islower (string[counter])) &amp;&amp; string[counter] != ' ')
  {
    printf ("[ERR] invalid characters\n");
    return -2;
  }
  
  //**************************************************************
  
  int difference = 0, shift = 0;
  for (int counter = 0; string[counter] != '\0'; counter++)
  {
  
    if (string[counter] &gt;= 'a' &amp;&amp; string[counter] &lt;= 'z') 
  {
  
  
    difference = 'z' - string[counter];
  
    if (Key &gt; difference)
      {
        shift = Key - (difference + 1);
        string[counter] = 'a';
        string[counter] += shift;
      }
    else
      {
        string[counter] += Key;
      }
  }
  }
  printf ("encrypted text: ");
  
  if (Key == 0)
  {
        string[strlen(string) - 1] = '\0';
        string[-1] = '\n';
    rev (string);
  
    return 0;
  }
  for (int counter = 0; string[counter] != '\0'; counter++)
  {
    printf ("%c", string[counter]);
  }
  }
  </code></pre>