A C-like approach was:
...
const int size = 20;
char buffer[size];
int n = snprintf(buffer, size, "%d", c); assert(n >= 0); assert(n < size); for(int i = 0; i < n; ++i) cout << buffer[i] << ' ';
What it does:
1) write the
c
into a character array (
buffer
).
2) make sure you do not write past the buffer end (use
snprintf[
^])
3) make sure the conversion does not need more characters than provided by the buffer (take in count the return value and
assert[
^] that writing was not expecting a larger buffer).
4) finally write each character followed by a space
A C++ like approach was to use
ostringstream[
^].
...
ostringstream oss;
oss << c;
assert(oss.good()); for(char ch: oss.str()) cout << ch << ' ';
string s = oss.str();
for(string::iterator it = s.begin(); it != s.end(); ++it) cout << *it << ' ';
Cheers
Andi