title: How can I correctly encrypt the input when there is a space?
body: <pre><code>#include &lt;iostream&gt;
  #include &lt;cstring&gt;
  #include &lt;string&gt;
  //#include &lt;cctype&gt;
  using namespace std;
  
  string k;
  int key[1024];
  int size = 0;
  int count = 0;
  char text[1024];
  char o;
  
  void encrypt(char input[]);
  void decrypt(char input[]);
  
  // get arguments from command line 
  int main(int argc, char *argv[])
  {
   if(argc &gt;= 3)
      {
        k = argv[2];
        // removing spaces from key 
        // storing key into char array
        for(int i = 0; i &lt; k.length(); i++)
      {
        if(k.at(i) == ' ')
          {
            key[i] = k.at(i+1);
                i++;
          }
        else
          {
            key[i] = k.at(i);
            // cout &lt;&lt; key[i] &lt;&lt; endl;
          }
  
        size++;
  
      }
        if(strcmp(argv[1], "-e") == 0)
      {
        // get text from user
        // encrypt
        cout &lt;&lt; "encryption " &lt;&lt; endl;
        cin.getline(text, sizeof(text));
        encrypt(text);
      }
        else if(strcmp(argv[1], "-d") == 0)
        {
          // get text from user
          // decrypt
          cout &lt;&lt; "decryption " &lt;&lt; endl;
          decrypt(text);
        }
  
      }
  
  }
  
  void encrypt(char input[])
  {
    string word = input;
    char wkey[word.length()];
    // cout &lt;&lt; word.length();
    for(int i = 0; i &lt; word.length(); count++, i++)
      {
        // if input is larger than the key
        // chars of key will repeat until the size is the length of the input
        if(i &gt; size - 1)
          {
            if(i == size)
          {
            count = 0;
          }
            wkey[i] = wkey[count];
          }
        else
          {
            wkey[i] = key[i];
          }
  
  
  
         // if input is a space, cout a space
        if(input[i] == ' ')
      {      
        cout &lt;&lt; " ";
  
      }
        // if input is something other than a letter, then just print it out
        else if(input[i] &lt; 65 || input[i] &gt; 122 || input[i] &gt; 90 &amp;&amp; input[i] &lt; 97)
      {
        cout &lt;&lt; input[i];
      }
        else
      {
        // ignoring case of the letters in the key
        // give the letters a range of 0-25
        if(wkey[i] &gt;= 65 &amp;&amp; wkey[i] &lt;= 90)
          {
            wkey[i]-= 'A';
          }
        else if(wkey[i] &gt;= 97 &amp;&amp; wkey[i] &lt;= 122)
          {
            wkey[i]-= 'a';
          }
        //      cout &lt;&lt; wkey[i] &lt;&lt; endl;
  
        // if input is uppercase, put it in the range of 0-25
        // make shift by adding to char in key
        // mod by 26 to wrap around
        // o is the encrypted character that will be printed
        if(input[i] &gt;= 65 &amp;&amp; input[i] &lt;= 90)
          {
            o = ((wkey[i] + (input[i] - 'A')) % 26) + 'A';
          }
        else if(input[i] &gt;= 97 &amp;&amp; input[i] &lt;= 122)
          {
            o = ((wkey[i] + (input[i] - 'a')) % 26) + 'a';
          }
      }
         cout &lt;&lt; o;
  
      } 
  }
  </code></pre>
  
  <p>The problem is that I am having trouble encrypting plaintext if that text contains a space. If the text is just one single word, then the program works. In the encryption function where I test the input for a space, I just print out a space, and then the next iteration of the for loop occurs. I think that this problem is occurring because once that next iteration occurs, the character in the key that is at the same index as the space in the input is skipped. I've tried doing an if statement to roll back to the skipped letter in the key, but I still get the wrong output. If anyone could give me some advice on how to fix this problem, I would really appreciate it.</p>