Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As shown <a href="http://www.cplusplus.com/reference/string/string/substr/" rel="noreferrer">here</a>, the second argument to <code>substr</code> is the <em>length</em>, not the ending position:</p> <blockquote> <p><code>string substr ( size_t pos = 0, size_t n = npos ) const;</code></p> <p><em>Generate substring</em></p> <p>Returns a string object with its contents initialized to a substring of the current object. This substring is the character sequence that starts at character position <code>pos</code> and has a length of <code>n</code> characters.</p> </blockquote> <p>Your line <code>b = a.substr(i,i+1);</code> will generate, for values of <code>i</code>:</p> <pre><code>substr(0,1) = 1 substr(1,2) = 23 substr(2,3) = 345 substr(3,4) = 45 (since your string stops there). </code></pre> <p>What you need is <code>b = a.substr(i,2);</code></p> <p>You should also be aware that your output will look funny for a number like 12045. You'll get <code>12 20 4 45</code> due to the fact that you're using <code>atoi()</code> on the string section and outputting that integer. You might want to try just outputing the string itself which <em>will</em> be two characters long:</p> <pre><code>b = a.substr(i,2); cout &lt;&lt; b &lt;&lt; " "; </code></pre> <p>In fact, the entire thing could be more simply written as:</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; using namespace std; int main(void) { string a; cin &gt;&gt; a; for (int i = 0; i &lt; a.size() - 1; i++) cout &lt;&lt; a.substr(i,2) &lt;&lt; " "; cout &lt;&lt; endl; return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload