Code:
sub = new char [count-startPosition];
to...
Code:
sub = new char [count+1];