title: c++ string subscript out of range
body: <p>Please help with debugging. It gives me an error 'string subscript out of range error'.</p>
  
  <p>The program needs to sort the text using insertion sort algorithm.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;string&gt;
  using namespace std;
  
  void insertionSort(string &amp;text, int size) {
    char temp;
    int i;
    for(int j=1;j&lt;size;j++)
    {
      //text.push_back(temp);
      temp=text[j];
      i=j-1;
     while(i&gt;=0 &amp;&amp; text[i]&gt;temp)
   {
  
   text[i+1]=text[i];
    i--;
   }
      text[i+1]=temp;
    }
  }
  
  int main()
  {
    string text="this a just text need to be sorted";
  
    int size = text.length();
    insertionSort(text,size);
    cout&lt;&lt;text&lt;&lt;endl;
    return 0;
  }
  </code></pre>
  
  <p>debug assertion Failed!</p>
  
  <p>Line:1441:</p>
  
  <p>Expression: string subscript out of range</p>
  
  <p>I supposed to change <code>text[i+1]=text[j]</code>  to <code>text[i+1]=text[i]</code>;</p>