How to make a number appear with its digits separated by spaces?

I have to make a program that reads an integer of any length and prints on screen this same number but with its digits separated by two spaces. This is the program I have made but I don't know what it fails at:

#include <iostream>
#include <cmath>
using namespace std;
int main(){
    setlocale(LC_CTYPE, "Spanish");
    long long numero, numero2;
    int contador;

    cout << "Introduzca un número: ";
    cin >> numero >> numero2;

    while(numero2>10){

        contador=0;

        while(numero2>10){
            numero2/=10;
            contador++;
        }
        cout << numero2 << "\t";

        for(contador=contador; contador==0; contador--){

            numero2=numero/(contador*10)-numero2*(10*contador);
        }

        numero=numero2;

    }
    system("pause");
}
 2
Author: Mariano, 2016-10-30

2 answers

How to make a number appear with its digits separated by spaces?

Extract digit by Digit and print it. Since we deal with numbers in base 10 it will be as easy as getting the residue of the division between 10. The problem is that this would display the number in reverse order, but it's easy to fix if we save the digits in a string of characters in reverse order:

std::string con_espacios(int numero)
{
    std::string result{};
    while (numero)
    {
        result.insert(0, 1, '0' + (numero % 10));
        result.insert(0, 1, '\t');
        numero /= 10;
    }

    return result;
}

The function con_espacios can be used like this:

std::cout << con_espacios(0x7fffffff) << '\n';
std::cout << con_espacios(666) << '\n';
std::cout << con_espacios(7'654'321) << '\n';

And shows what next:

  2   1   4   7   4   8   3   6   4   7
  6   6   6
  7   6   5   4   3   2   1

If you want to avoid tabbing too much, do not insert it if the number is less than 10.

You can also use the recursive version:

std::string con_espacios(int numero)
{
    const char digito = '0' + (numero % 10);
    const std::string result({digito, '\t'});

    if (numero < 10)
        return result;

    return con_espacios(numero / 10).append(result);
}

The trick of the recursive version is that undoing the recursion concatenates (std::basic_string::apend) from end (most significant digit) to beginning (least significant digit).

Note that this version of the function adds the extra tab at the end rather than at the beginning. By finally, you can pass the number to string and copy it into a string with spaces by putting the digits in the odd positions:

std::string con_espacios(int numero)
{
    std::string texto = std::to_string(numero);
    std::string result(texto.size() * 2, ' ');

    int indice = 0;

    for (const auto &digito : texto)
    {
        result[indice] = digito;
        indice += 2;
    }

    return result;
}
 1
Author: PaperBirdMaster, 2018-05-10 08:23:50

I think it is to complicate it in vain, do not treat them as numbers if you are not going to perform another operation other than printing them on screen. Instead, you might want to treat them as strings. Here is a simple example:

#include <iostream>
#include <cmath>
using namespace std;
int main(){
    string numero;
    int contador;

    cout << "Introduzca un numero, 0 para terminar\n";
    cin >> numero;

    while(numero != "0"){
        for (int i = 0; i < numero.length(); i++)
            cout << numero[i] << " ";
        cout << endl;

        cout << "Introduzca un número, 0 para terminar\n";
        cin >> numero;
    }
}

I hope it served you.

Greetings!

 4
Author: Genarito, 2016-10-30 14:19:45