rendered paste body#include <iostream>
#include <cstdio>
#include <cstdlib>
#ifdef WIN32
# include <conio.h>
# define clrscr() system("cls")
# define pause() getch()
#else
# define clrscr() printf("\033c")
# define pause() getchar()
#endif
char *getline();
char *toBinary(char *);
char *reverse(char *);
int main(int argc, char *argv[]) {
while(1) {
clrscr();
printf("\n%s", toBinary(getline()));
pause();
}
return EXIT_SUCCESS;
}
char *getline() {
char *buf = NULL, tmp[] = " ";
while((tmp[0] = getc(stdin)) != '\n' && tmp[0] != EOF) {
buf = (char *)realloc(buf, ((buf)?strlen(buf):0) + 2);
strcat(buf, tmp);
}
return buf;
}
char *toBinary(char *str) {
unsigned short ch;
char *buf = " ";
for(int i=0;i<strlen(str);i++) {
ch = str[i];
while(ch > 0) {
buf = (char *)realloc(buf, ((buf)?strlen(buf):0) + 2);
strcat(buf, (ch%2)?"1":"0");
ch /= 2;
}
}
return reverse(buf);
}
char *reverse(char *str) {
char *buf = (char *)malloc(strlen(str) + 1), tmp[] = " ";
for(int i=strlen(str)-1;i>=0;i--) {
tmp[0] = str[i];
strcat(buf, tmp);
}
return buf;
}