Ex.
12341243123 -> "12341243123"
int at_num(int num, int at) {
return (num / int(pow(10, at))) % 10;
}
void int_to_string(int n, string &str, int at) {
int i = at_num(n, at);
if (i != 0) {
str.insert(0, to_string(i));
return int_to_string(n, str, ++at);
}
}
string int_to_string(int n) {
string result = "";
int_to_string(n, result, 0);
return result;
}
int main() {
cout << int_to_string(1234);
pause();
}
int digit_at(const long long num, const int at, const size_t size)
{
return int(num / pow(10, size - at - 1)) % 10;
}
void to_string(const long long num, const int &size, string &final)
{
static int count = 0;
if (count < size) {
final += digit_at(num, count++, size) + '0';
to_string(num, size, final);
}
count = 0; // otherwise next function call maintain this.
}
string my_to_string(const long long num)
{
string final;
const int size = num_digits(num);
final.reserve(size);
to_string(num, size, final);
return final;
}
int main()
{
cout << my_to_string(1234) << endl;
cout << my_to_string(123456) << endl;
cout << my_to_string(123456789) << endl;
}