reverse a number of digits of the user input

Hello,

I want to make a program where the user has to input a few positive integers, like: 12345.
Then I want to reverse that to: 54321.
I have to use itoa and atoi, but I am not familiar with the operation of atoi and itoa...
To reverse the digits I have to make use of an iteration function like for, while etc....
Here is my code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <stdlib.h>

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

  c=atoi(buffer);
  printf("%d",buffer[d]);
           

  getchar();
  getchar();
}


Can someone help me with the iteration and reversing the digits.
If I read in the integers and store that in the variable i. Then itoa is going to make a string of those integers. That's all I know about it..
Looks like you're doing it right so far. Line 18 should probably print c rather than the buffer (which is using d, an uninitialized variable).

There are two thoughts about reversing a c-string: 1) create a new buffer and copy the contents of the old into it, but starting from the back. Then copy the new buffer into the old. 2) Initialize variables the first and last index of the buffer. Swap the data at these indecies, increase the first, decrease the last. Repeat until the first is not less than the last ( they will sort of meet in the middle).
Try the following

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
while ( 1 )
{
	const unsigned int base = 10;
	unsigned int x = 0, y = 0;

	printf( "Enter a non-negative number (0 - exit): " );
	scanf( "%u", &x );

	if ( !x ) break;

	do { y = base * y + x % base; } while ( x /= base );


	printf( "The reverse number: %u\n", y );
}
@LowestOne you mean something like:

1)
1
2
3
help=buffer[i];
      buffer[i]=buffer[i-1];
     buffer[i-1]=help;


Only the second thought I don't understand. Do I have to use an for loop then?


@vlad from moscow

Well it works fine but that is not my question..
Last edited on
@dutchman
Well it works fine but that is not my question..


Is it your question "can someone help me with the iteration and reversing the digits.", is not it? :)
you got me there, but nog in the way you programmed it.
LowestOne you mean something like: ...


Not really, kindof. The code you posted would be a swap, something you would need for my #2. The indecies don't make sense though. #1 doesn't need a "help", because your reverse is stored in an entirly new array.

You need a loop for both, can't really post them because that's the thing you're trying to learn :)

#2 is a better approach, I would say:
1
2
3
4
5
6
7
8
9
10
11
12
char buffer[6] = {'1','2','3','4','5','\0'};
int first = 0;
int last = strlen(buffer) - 1;

// To do the entire array, you need a loop
// Where first is increased and last is decreased until first >= last 

char store = buffer[first];
buffer[first] = buffer[last];
buffer[last] = store;

printf("%s\n", buffer);




Last edited on
what does strlen mean?
I guess this is what you mean:


1
2
3
4
5
6
  for(first=0; first<=33; first++)
               for(last=33; last>=first; --last)
  
  store=buffer[first];
  buffer[first]=buffer[last];
  buffer[buffer-1]=store;
@LowestOne

Am I on the right track?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>
#include <stdlib.h>

int main ()
{
  
  
  int help=0;
  int i;
  int b;
  int d;
  char buffer[5];
  
  
  printf ("Enter numbers: ");
  scanf ("%d",&i);
  itoa(i,buffer,10);
  
  for(d=0; d<5; d++)
  {
           help=buffer[d];
           buffer[d]=buffer[5-d-1];
           buffer[5-d-1]=help;
           }
  for(d=4; d>=0; --d)
  {
           b=atoi(buffer);
           printf("%d",b);
}
  getchar();
  getchar();
}
Topic archived. No new replies allowed.