Afficher un message
Vieux 30/05/2008, 21h34   #1
hank
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How to print an array of char backward.

I have this code here, it converts decimal numbers to binary. There is
one problem. The output is printed 'in reverse' and I have no clue at all
how to solve this problem.

Output of program:

Decimal number: 10
Binary representation: 0101
(It should be 1010, and not 0101...)

I would be glad if someone could me. Maybe there is some function,
which does the trick... (I have no idea.)

Thanks.

/************************************************** ********************/
#include <stdio.h>
#include <string.h>

int main()
{
char bin[25] = "";
int dec;

printf("Decimal number: ");
scanf("%d", &dec);

do {
if (dec % 2 == 0)
strcat(bin, "0");
else {
strcat(bin, "1");
dec--;
}
dec = dec / 2;
} while (dec > 0);
printf("Binary representation: %s\n", bin);

return 0;
}
/************************************************** ********************/
  Réponse avec citation
 
Page generated in 0,05302 seconds with 9 queries