Split an integer number into digits non-backward

I am looking for an efficient and simple way to separate an integer into digits, but I need to separate them starting from the first one.

Here's the typical way to sepparate a number into digits:

int main(int argc, char* argv[]) { //argc is going to be the number
    while(argc != 0) {
        cout << argc % 10 << endl;
        argc /= 10;
    }

    return 0;
}

But doing it this way I will obtain, for example, 5437 -> 7,3,4,5 I want to reverse the order of appereance, 5,4,3,7. So I created this:

int main(int argc, char* argv[]) {
    int rem = 1;

    while(argc / rem > 10) //We increase the remainder until we get the same amount of digits than the number
        rem *= 10;

    while(argc != 0) {
        cout << argc / rem << endl; //We take the cocient of the division, wich will be always the first digit
        argc %= rem; //Now we discard that digit
        rem /= 10; //And we decrease the number of digits of the remainder too
    }

    return 0;
}

The thing is: is there any alternative way to do this in a shorter/simpler way?

PS: I can't use stacks, lists or that kind of structures.

Thanks in advance

Upvotes: 2

Views: 457

Answers (4)

Jordan Harris
Jordan Harris

Reputation: 390

I think I would convert the int to a string, go through each char and convert that back to an int, while storing that newly created int in a vector. This may be a lot more complicated than it has to be, but I find it easier. Mostly because I already made functions that convert a digit to string and vice versa. I'm sure this isn't an efficient way of doing it either, but it works.

Here's how I just did it (note that this could all be done in a single function):

// convert an int to a string representation
string intToString(const int& digit)
{
    ostringstream oss;
    oss << digit;
    return oss.str();
}

// if possible, convert characters in string to integer value
int stringToInt(const string& str)
{
    istringstream iss{ str };
    int digit;
    iss >> digit;
    if (!iss)
        throw runtime_error{ "integer format error: " + str};
    return digit;
}

// split an int into its seperate digits and store them in a vector
vector<int> splitInteger(const int& digit)
{
    vector<int> splits;     // holds the ints that are split from the original
    const string s = intToString(digit);
    for (char ch : s) {
        const string temp{ ch };    // convert char to string for use with stringToInt()
        splits.push_back(stringToInt(temp));
    }
    return splits;
}

You might also want to name things better than I did, if you do go this route. I'm terrible at quickly naming things. :)

So here's a simple way of using splitInteger():

int main()
{
    cout << "Enter an integer: ";
    int num;
    cin >> num;
    vector<int> splits = splitInteger(num);

    for (const int& i : splits)
        cout << i << '\n';

    system("pause");
    return 0;
}

Upvotes: 0

AlexD
AlexD

Reputation: 32566

unsigned n = 123450;
unsigned m = 0, k = 1;
while (n != 0) {
    m = m * 10 + n % 10;
    n /= 10;
    k++;
}
while (--k > 0) {
    cout << m % 10 << endl;
    m /= 10;
}

Upvotes: 0

6502
6502

Reputation: 114461

A solution could be recursion:

int index=0, result[100];

void digits(int x) {
    if (x > 10) digits(x/10);
    result[index++] = x % 10;
}

(it would use an implicit stack, however)

Upvotes: 0

Ashouri
Ashouri

Reputation: 906

You can also use some string methods .For instance you can convert your number to a string .Therefor, you can use string splitters functions. of course you can use below sample code

#define MaxDigits 8

void SplitDigits(int Number, int* DigitArray)
{
   for(int i=(MaxDigits-1); i>=0; i++)
   {
      DigitArray[i] = Number % 10;
      Number /= 10;
   }
}

int main()
{
   int DigitArray[MaxDigits];
   int Number = 1538;

   SplitDigits(Number, DigitArray);

   for(int i=0; i<MaxDigits; i++)
   {
      cout << DigitArray[i] << endl;
   }

   return 0;
}

Upvotes: 1

Related Questions