This loop is invalid
for (i = len - 1; i >= 0; --i)
The condition will be always equal to true because expression --i will be always >= 0 due to the fact that i is unsigned integer.
Also when str.size() is equal to zero then len - 1 will be equal to std::string::npos.