reverse digits

Hi people,

I want to read in positive digits and then I want to reverse that. But, in order to do that I have to convert it into a string. Than reverse that string and than convert it back to digits.
I have to use itoa and atoi.
Here is what I've got:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <stdlib.h>

int main ()
{
  int i;
  int a;
  int c;
  int help;
  char buffer [5];
  
  printf ("Enter numbers: ");
  scanf ("%d",&i);
  itoa(i,buffer,10);
  

  c=atoi(buffer);
  
  printf("%d",......);
  getchar();
  getchar();
}


Last edited on
In C++, we might do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <sstream>
#include <iostream>
#include <string>
#include <algorithm>

std::string to_string( unsigned value )
{
    std::ostringstream os ;
    os << value ;
    return os.str() ;
}

int main()
{
    unsigned input ;

    std::cin >> input ;
    std::string s = to_string(input) ;
    std::reverse(s.begin(), s.end()) ;

    std::cout << s << '\n' ;
}
Last edited on
I don't know how to use the above programming style of c++. But I can't use recursive only iterative.
Topic archived. No new replies allowed.