446,389 Members | 1,795 Online Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

# Reversing digits

 P: 6 Hi, I'm rather new at C++, and I'm having problems wrapping my brain around this problem. What I'm supposed to do is as follows: Write a function that takes an arbitrary integer value and returns the number with its digits reversed. For example, given the number 12345 the function should return 54321. We assume that the reverse of 1000 can be 1 because leading zeros are omitted. Similarly, the reverse of 000123 is 321. We're learning about arrays in my class right now, so I'm assuming that the code has to do with fitting each digit into its own array component. I'm pretty sure I know how I could reverse each digit were placed in its own component in array, but my problem has to do with putting each digit into it's own component. Any ideas as to how I could go about doing that? Thanks so much! Nov 7 '06 #1
4 Replies

 Expert 100+ P: 1,510 You need to convert your int value to an array of char where you can reverse the digits. Using C++ you can use to do this Expand|Select|Wrap|Line Numbers     int i=123456;     // convert i to a string and put it in outstring     char outstring={};     ostrstream strout(outstring, 20);     strout << i;   the array outsting[] would contain the digit characters so outstring would be '1' outstring would be '2' outstring would be '3' etc Nov 7 '06 #2

 P: 9 Hi there, well i suppose that if you are learning arrays right now in your class then, the problem cannot be solved by using complex data structures like linked list. Have u done linked lists? So tell me so that i can help u with the solution. if not then there has to be an upper bound on the length of integer input. Do ask this from your teacher. Keep cool, Nov 7 '06 #3

 P: 6 Okay, thanks for the help Horace1; I've implemented what you suggested into my code. In response to seeminsuleri, I haven't gone so far as to learn linked lists yet, and I think my teacher gave me an upper bound of 5 digits. So here's what was assigned of me, and this is what I came up with: Write a function that takes an arbitrary integer value and returns the number with its digits reversed. For example, given the number 12345 the function should return 54321. We assume that the reverse of 1000 can be 1 because leading zeros are omitted. Similarly, the reverse of 000123 is 321. Write a main program that illustrates the work of this function. If the given number is equal to the reversed number (e.g. 12321) the main program should display a message it is a palindrome!. Expand|Select|Wrap|Line Numbers #include  #include  using namespace std;   void reverse (char a[], int& i, int& na) {     int j, t;     for (i=0,j=na-1;i> x;     na = 5;     separator (a, x);     cout << "\nInput number: ";     for (i=0;i

 P: 2 #include void main(void) { int a,an; int i,na,n,t=0,j; cout<<"enter no"; cin>>na; j=na; cout<<"array"; for(i=0;i>a[i]; } for(i=0;i=0;i--) { cout< 