0

I've been trying to find how to do it online. I'm restricted from not using ready-made functions, like to_string or boost::lexical_cast, not even the <sstream> library. How can I do it with these limitations?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
I_Burn
  • 31
  • 5
  • Does this answer your question? [Easiest way to convert int to string in C++](https://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c) – JHBonarius Sep 10 '20 at 19:42
  • 2
    please learn to use the search function of this site or Google. – JHBonarius Sep 10 '20 at 19:43
  • Maybe check this post: [https://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c](https://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c). – Clémence CHOMEL Sep 10 '20 at 19:43
  • 1
    One digit at a time? I would guess whoever gave you the assignment with the restrictions also gave some guidance on how to start. – Retired Ninja Sep 10 '20 at 19:43
  • @JHBonarius Nope apparently, because I was told that the sstream isn't allowed, neither is to_string or anything else like that. I tried arguing that sstream is a needed input output library but NoOoOoO, you can't do that. It's really pissing me off tbh. – I_Burn Sep 10 '20 at 19:48
  • @RetiredNinja No, I wasn't given any guidance about it. As for the solution, do you mean casting it to a char and then string? – I_Burn Sep 10 '20 at 19:51
  • 3
    you have a stupid teacher ( I hate teachers that act they are teaching you C++, but don't allow you to actual use C++.). you can try `char` and doing `'0'+i` per digit... i.e. using the char for 0 and adding an offset. – JHBonarius Sep 10 '20 at 19:53
  • @JHBonarius Oh man tell me about it, he's a real pain sometimes. Also thanks for the answer, appreciate it. – I_Burn Sep 10 '20 at 19:55
  • 1
    To be fair, I had to implement the equivalent manually once for a real-life program. Around 2005 or so. For underpowered cell phones. – Karl Knechtel Sep 10 '20 at 20:02
  • @KarlKnechtel atoi or to_string should be a very efficient solution, so wonder how you would improve on that. – JHBonarius Sep 11 '20 at 06:57
  • Well, you see, I was doing it in J2ME, and.... – Karl Knechtel Sep 14 '20 at 06:20

2 Answers2

2

Here's one way to do it:

std::string int_to_string (int i)
{
    bool negative = i < 0;
    if (negative)
        i = -i;
    std::string s1;

    do
    {
        s1.push_back (i % 10 + '0');
        i /= 10;
    }
    while (i);
    
    std::string s2;
    if (negative)
        s2.push_back ('-');
    for (auto it = s1.rbegin (); it != s1.rend (); ++it)
        s2.push_back (*it);
    return s2;
}

I avoided using std::reverse, on the assumption that it would be off-limits.

Live demo

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
1

You can use '0' + i to get the char value of 0 and offset it. I.e.

#include <cmath>
#include <string>
#include <iostream>

int main() {
    int number = 12345678;

    int nrDigits = std::log10(number)+1;

    std::string output(nrDigits, '0'); // just some initialization

    for (int i = nrDigits - 1; i >= 0; i--) {
        output[i] += number % 10;
        number /= 10;
    }

    std::cout << "number is " << output << '\n';
}
JHBonarius
  • 10,824
  • 3
  • 22
  • 41