title: C++ string returning a space when accessing a valid index containing a P
body: <p>I have the following assignment:</p>
  
  <blockquote>
    <p>The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p>
  
  <pre><code>P   A   H   N
  A P L S I I G
  Y   I   R
  </code></pre>
    
    <p>And then read line by line: "PAHNAPLSIIGYIR"</p>
    
    <p>Write the code that will take a string and make this conversion given a number of rows:</p>
    
    <p><code>string convert(string s, int numRows);</code></p>
    
    <p>Example 1:</p>
    
    <p>Input: s = "PAYPALISHIRING", numRows = 3<br>
    Output: "PAHNAPLSIIGYIR"</p>
    
    <p>Example 2:</p>
    
    <p>Input: s = "PAYPALISHIRING", numRows = 4<br>
    Output: "PINALSIGYAHRPI"<br>
    Explanation:</p>
  
  <pre><code>P     I    N
  A   L S  I G
  Y A   H R
  P     I
  </code></pre>
  </blockquote>
  
  <p>The following is what I tried:</p>
  
  <pre><code>std::string convert(std::string s, int numRows) {
          std::string answer = "";
          std::vector&lt;int&gt; row1_indicies;
          for (int i = 0; i &lt; s.length(); i++)
          {
              if (numRows % 2 == 0)
              {
                  row1_indicies.push_back(numRows*i+2*i);
              }
              else
              {
                  row1_indicies.push_back(numRows*i+i);
              }
              if (row1_indicies[i] &gt; s.length())
              {
                  row1_indicies.pop_back();
                  break;
              }
              else if (row1_indicies[i] == s.length())
              {
                  break;
              }
          }
          for (int i = 0; i &lt; numRows; i++)
          {
              for (int j = 0; j &lt; row1_indicies.size(); j++)
              {
                  if (i == 0)
                  {
                      answer += s[row1_indicies[j]];
                  }
                  else if (i == numRows-1)
                  {
                      answer += s[row1_indicies[j]+i];
                  }
                  else
                  {
                      if (j == 0)
                      {
                          answer += s[row1_indicies[j]+i];
                      }
                      else
                      {
                          answer += s[row1_indicies[j]-i];
                          answer += s[row1_indicies[j]+i];
                      }
                  }  
              }
          }
          return answer;
      }
  
  int main(){
      std::string input = "PAYPALISHIRING";
      int numRows = 4;
      std::cout &lt;&lt; "input = " &lt;&lt; input &lt;&lt; ", number of rows = " &lt;&lt; numRows &lt;&lt; std::endl;
      std::string output = convert(input, numRows);
      std::cout &lt;&lt; output &lt;&lt; "    expected: PINALSIGYAHRPI" &lt;&lt; std::endl &lt;&lt; std::endl;
  
      return 0;
  }
  </code></pre>
  
  <p>My error occurs in the block:</p>
  
  <pre><code>else if (i == numRows-1)
  {
     answer += s[row1_indicies[j]+i];
  }
  </code></pre>
  
  <p>When I debug I get "PINALSIGYAHR PI". It shows /000P is being returned when that index is accessed.
  I added the following check to the block:</p>
  
  <pre><code>else if (i == numRows-1)
  {
     std::cout &lt;&lt; std::endl &lt;&lt; s.at(row1_indicies[j] + i) &lt;&lt; std::endl;
     answer += s[row1_indicies[j]+i];
  }
  </code></pre>
  
  <p>Output looks like:</p>
  
  <pre><code>input = PAYPALISHIRING, number of rows = 4
  
  P
  
  I
  
  terminate called after throwing an instance of 'std::out_of_range'
    what():  basic_string::at: __n (which is 15) &gt;= this-&gt;size() (which is 14)
  </code></pre>
  
  <p>Any ideas?</p>