title: How substr() of c++ really works?
body: <p>I'm trying to print the substring of a string but the substr() function's behaviour seems odd to me.</p>
  <p>In my below program, I'm trying to print the substring of a string with value &quot;Kamal&quot;.</p>
  <p>Here, substr(0,4) prints &quot;Kama&quot; which makes me assume that 0 is inclusive and 4 is exclusive.
  But substr(1,4) prints &quot;amal&quot; which violates my above understanding since the character in position 4 is printed while in previous case it's not.
  Can you explain this strange behaviour.</p>
  <p>If this question is really dumb, sry this is the first time I'm using substr(). I've searched the net but can't seem to find any answer useful.</p>
  <pre><code>#include&lt;iostream&gt;
  #include&lt;string&gt;
  using namespace std;
  
  int main() {
      string s1 = &quot;Kamal&quot;;
  
      cout &lt;&lt; s1.substr(0, 4) &lt;&lt; '\n';
      cout &lt;&lt; s1.substr(1, 4) &lt;&lt; '\n';
  
      return 0;
  }
  </code></pre>
  <p>Output:
  <a href="https://i.stack.imgur.com/9IubY.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9IubY.jpg" alt="enter image description here" /></a></p>