1

I want to reverse the number given by the user. I wrote the code in such a way that it takes the number of digits of number and the input and reverses it. As even the limit of 'long long' is 19 digits, what can I do so that the code works even if the number of digits in the input are greater than 20? [Without using third party libraries]

#include<iostream>
using namespace std;

void make_int(int a[],long long int n)
{
    int i=0;
    while(n)
    {
        a[i]=n%10;
        i++;
        n=n/10;
    }
    for(int j=0;j<i;j++)
        cout<<a[j];

}

int main()
{

    int N;
    cin>>N;
    int *tc = new int[N];
    long long int num;
    cin>>num;
    make_int(tc,num);
    return 0;
}
Wasim Thabraze
  • 780
  • 1
  • 12
  • 29
  • 2
    You use `std::string` instead: `std::string n; cin >> n; std::reverse(n.begin(), n.end()); cout << n;` – The Paramagnetic Croissant May 26 '14 at 19:40
  • 1
    I find it hard to believe that you didn't try Google before posting this question -as it is a very popular interview question. But turning the number into an array with each digit filling one element of the arrray is one way to go – Scott Selby May 26 '14 at 19:40

1 Answers1

3

Read the number in as a std::string and reverse that, since it's actual numerical value is unimportant for what you want to do.

ooga
  • 15,423
  • 2
  • 20
  • 21
  • 1
    a string is still going to have a limit , the question was hypothetically a number of *any* length – Scott Selby May 26 '14 at 19:41
  • 3
    @ScottSelby that's not possible with a computer of finite computational capacity and resources. – The Paramagnetic Croissant May 26 '14 at 19:42
  • 5
    I thought a string can only be 509 char long in c ?? was I wrong? – Scott Selby May 26 '14 at 19:43
  • I understand computers have limits and I'm not going into conceptual craziness by assuming you may need to input a number with 2,000 digits which will exceed a string's max – Scott Selby May 26 '14 at 19:47
  • 2
    @ScottSelby An `std::string` stores (long enough) strings on the heap, its limit is the RAM it can get from the OS. So a billion digits should not cause a problem nowadays. – Baum mit Augen May 26 '14 at 19:51
  • ahh, i see , the best answer I have always found to that question is to turn it into an array - although I am usually focused on c# , and I do not know what the c# comparison of std::string is- – Scott Selby May 26 '14 at 19:53
  • 2
    @ScottSelby Since I usually use C++ I don't know either. :) And here http://coliru.stacked-crooked.com/a/0c38318abb580a7a is an example with 350! (740 digits), works like a charm. – Baum mit Augen May 26 '14 at 19:55
  • 1
    @ScottSelby: In C, 509 is the limit for a [string literal](http://stackoverflow.com/questions/11488616/why-is-max-length-of-c-string-literal-different-from-max-char), that is a read-only string compiled into the object's data section. A C string, i.e. an array of chars, can be of any length that fits into memory. – M Oehm May 26 '14 at 20:01
  • 1
    That 509 number is not an upper bound. – R. Martinho Fernandes May 26 '14 at 21:43