string temp = leftSide.substr(index, (index+1));
substr takes the first index and then a length, rather than first and last indices. You want substr(index, 2)
. Since in your example index
is 0 you're doing: substr(index, 1)
which creates a string of length 1, which is "C"
.
string temp = leftSide.substr(index, index);
Since index
is 0 this is substr(index, 0)
, which creates a string of length 0, that is, an empty string.
When you're processing parts of the string with a higher index, such as Fe
in "CuFe3"
the value you pass in as the length parameter is higher and so you're creating strings that are longer. F
is at index 2 and you call substr(index, 3)
, which creates the string "Fe3"
.
Also the standard library usually uses half open ranges, so even if substr took two indices (which, again, it doesn't) you would do substr(index, index+2)
to get a two character string.
bool NEXT_LOWER_LETTER = islower(leftSide[index+1]);
You might want to check that index+1
is a valid index. If you don't want to do that manually you might at least switch to using the bounds checked function at()
instead of operator[]
.