By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,795 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
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 <strstream> to do this
Expand|Select|Wrap|Line Numbers
  1.     int i=123456;
  2.     // convert i to a string and put it in outstring
  3.     char outstring[20]={};
  4.     ostrstream strout(outstring, 20);
  5.     strout << i;
  6.  
the array outsting[] would contain the digit characters so
outstring[0] would be '1'
outstring[1] would be '2'
outstring[2] would be '3'
etc
Nov 7 '06 #2

seeminsuleri
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
  1. #include <iostream>
  2. #include <strstream>
  3. using namespace std;
  4.  
  5. void reverse (char a[], int& i, int& na)
  6. {
  7.     int j, t;
  8.     for (i=0,j=na-1;i<j;i++,j--)
  9.     {
  10.         t=a[i];
  11.         a[i]=a[j];
  12.         a[j]=t;
  13.     }
  14. }
  15.  
  16. void separator (char a[], int x) //Compliments of Horace1
  17. {
  18.     // convert i to a string and put it in outstring
  19.     ostrstream strout(a, 20);
  20.     strout << x;
  21. }
  22.  
  23. int main ()
  24. {
  25.     char a[20];
  26.     int na, i, j, x, y;
  27.     cout << "Enter five-digit number:  ";
  28.     cin  >> x;
  29.     na = 5;
  30.     separator (a, x);
  31.     cout << "\nInput number: ";
  32.     for (i=0;i<na;i++)
  33.         cout << a[i];
  34.     cout << "\nReverse number: ";
  35.     reverse (a, i, na);
  36.     for (i=0;i<na;i++)
  37.         cout << a[i];
  38.     for (i=0,j=na-1;i<j;i++,j--)
  39.         if (a[i]!=a[j])
  40.         {
  41.             cout << "\nThis number is NOT a palindrome!\n";
  42.             break;
  43.         }
  44.         else continue;
  45.     if (i==j)
  46.         cout << "\nThis number is a palindrome!\n";
  47.  
  48.     return 0;
  49. }
  50.  
There it is. I apologize for the amateurish-ness of it. Thanks for the help!
Nov 7 '06 #4

P: 2
#include<iostream.h>

void main(void)
{
int a[10],an[10];
int i,na,n,t=0,j;
cout<<"enter no";
cin>>na;
j=na;
cout<<"array";
for(i=0;i<na;i++)
{
cin>>a[i];
}
for(i=0;i<na;i++)
{
if(a[i]==0)
{
for(n=i;n<na;n++)
{

a[n]=a[++n];
//cout<<a[n];
};
j--;
}
else
{
an[t++]=a[i];
}
}
for(i=--j;i>=0;i--)
{
cout<<an[i];
}
}
Nov 7 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.