Start by iterating over the string: that's easy! std::string has an operator[] overload, so you can access each character one-by-one:
std::string myStr = "Hello!";
for(unsigned int i = 0; i < myStr.length(); i++)
{
char c = myStr[i]; cout << c;
}
So...adding a comma after each three characters? Not complex, really:
int commaAt = 3;
std::string myStr = "Hello there!";
for(unsigned int i = 0; i < myStr.length(); i++)
{
if (commaAt == 0)
{
cout << ',';
commaAt = 3;
}
char c = myStr[i]; cout << c;
commaAt--;
}
"if i wanted to return a string rather than cout-ing how can i do that?
It was doing pretty much what you show, but trying to concat the chars to another string where I started to get exceptions..."
That's also pretty easy, when you think about what you are doing:
int commaAt = 3;
std::string myStr = "Hello there!";
std::string output = "";
for(unsigned int i = 0; i < myStr.length(); i++)
{
if (commaAt == 0)
{
output += ',';
commaAt = 3;
}
commaAt--;
char c = myStr[i]; output += c;
}