Use std::string::empty() instead of comparing length to zero

Also use std::string::back() instead of indexing with length minus one.