Reversing digits

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ipazman420
    New Member
    • Nov 2006
    • 6

    Reversing digits

    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!
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    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
    Code:
        int i=123456;
        // convert i to a string and put it in outstring
        char outstring[20]={};
        ostrstream strout(outstring, 20);
        strout << i;
    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

    Comment

    • seeminsuleri
      New Member
      • Nov 2006
      • 9

      #3
      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,

      Comment

      • ipazman420
        New Member
        • Nov 2006
        • 6

        #4
        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!”.

        Code:
        #include <iostream>
        #include <strstream>
        using namespace std;
        
        void reverse (char a[], int& i, int& na)
        {
        	int j, t;
        	for (i=0,j=na-1;i<j;i++,j--)
        	{
        		t=a[i];
        		a[i]=a[j];
        		a[j]=t;
        	}
        }
        
        void separator (char a[], int x) //Compliments of Horace1
        {
            // convert i to a string and put it in outstring
            ostrstream strout(a, 20);
            strout << x;
        }
        
        int main ()
        {
        	char a[20];
        	int na, i, j, x, y;
        	cout << "Enter five-digit number:  ";
        	cin  >> x;
        	na = 5;
        	separator (a, x);
        	cout << "\nInput number: ";
        	for (i=0;i<na;i++)
        		cout << a[i];
        	cout << "\nReverse number: ";
        	reverse (a, i, na);
        	for (i=0;i<na;i++)
        		cout << a[i];
        	for (i=0,j=na-1;i<j;i++,j--)
        		if (a[i]!=a[j])
        		{
        			cout << "\nThis number is NOT a palindrome!\n";
        			break;
        		}
        		else continue;
        	if (i==j)
        		cout << "\nThis number is a palindrome!\n";
        
        	return 0;
        }
        There it is. I apologize for the amateurish-ness of it. Thanks for the help!

        Comment

        • pavanikondru
          New Member
          • Oct 2006
          • 2

          #5
          #include<iostre am.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];
          }
          }

          Comment

          Working...