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 help 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;
}
/**********************************************************************/
.



Relevant Pages

  • Re: How to print an array of char backward.
    ... The output is printed 'in reverse' and I have no clue at ... You could reverse the string in place before printing it. ... int main{ ...
    (comp.lang.c)
  • Re: Problems with my little c-program
    ... A pointer is not an int. ... By the way, you're not using a stack here, in the conventional sense. ... and when you reverse the file you're ...
    (comp.lang.c)
  • Re: Highly efficient string reversal code
    ... written this code to reverse a string in place. ... int reverse{ ... size_t lgh; ... char * my_strrev{ ...
    (comp.lang.c)
  • Re: SPOILER: Independent 26/05/08 (was Re: SCWC 52 Results)
    ... *not* a definition and no amount of saying it loud will make it one. ... As I understand it the debate is whether the clue ... cryptic crossword purposes. ... the reverse of a definition - DIAL + a reversal indicator in the clue where ...
    (rec.puzzles.crosswords)
  • SCWC5 results
    ... where cryptic clues were posted for FLOWER. ... Congratulations to TockMurtle for this winning clue: ... The indication for "hidden in reverse" is just too loose. ... Set temperature a degree lesser for development of bud ...
    (rec.puzzles.crosswords)