I guess that what you're trying to achieve is this:
std::string letterToNumber(std::string input) {
toUpper(input);
std::stringstream output;
std::string::iterator it;
for (it = input.begin(); it != input.end(); ++it) {
if (input.begin() != it) {
output << ",";
}
int letterIndex = static_cast<int>(*it) - 'A';
output << letterIndex;
}
return output.str();
}
- It looks simpler and more efficient to me to build a new string rather than try to edit the existing one, because since letters (1 char) map to several chars, your initial string would need several inefficient copies and reallocations.
- To convert from a character to its index, you can use the fact that ASCII characters are naturally ordered and contiguous.
- You can add protection for non-letter characters, e.g. numbers and most punctuation will return negative indexes