low as in the least significant. So 5414412... the low digit is 2.
You have the right idea in your code.. but there are a few mistakes/things I'd change:
- you're using 'i' in that nested loop, but it looks like you meant to use 'j' instead (see lines 17, 18)
- if you're including index 0 in your loops (
i >= 0
) then you can't index i-1 because that will go out of bounds when i==0. Your best bet is to put an extra '0' at the start of your string (
t = ")" + s;
) and do
i > 0
in your loops.
- it doesn't look like you're changing t properly. You appear to be inserting characters when you divide rather than replacing characters.
- push_front is preferable to insert. Although if you're doing a lot of insertions this way, vector isn't the best choice (I'm assuming that's what vbit is). In fact, vector might be the very worst choice. You may want to consider deque instead.
- you don't need this atoi/itoa nonsense. To convert from a character to a number you just subtract '0':
1 2
|
i = atoi(c); // boo
i = c - '0'; // yay
|
Likewise, you can add '0' to convert back to a character.
Not to mention I don't think your 'buffer' is big enough for atoi/itoa calls anyway (only 1 char wide, no room for the null terminator).