Skip to content

Instantly share code, notes, and snippets.

@johnhmj
Created January 2, 2010 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnhmj/267380 to your computer and use it in GitHub Desktop.
Save johnhmj/267380 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFERSIZE 1024
//
void sample(void)
{
// size_t is unsigned int type.
size_t str_size = 0, i = 0;
int alphabet = 0;
// string: char array
// You should initialize them for safety.
char word[BUFFERSIZE] = "", wordindex[BUFFERSIZE] = "";
// You may need a buffer.
char buffer[BUFFERSIZE] = "";
printf("Enter your desired word or sentence: ");
// string word, its name is a pointer.
//scanf("%s", word);
// get a string for a line.
gets(word);
// let strlen return size_t type.
str_size = strlen(word);
for (i = 0; i < str_size; i ++)
{
alphabet = (int)word[i];
itoa( alphabet, buffer, 2);
strcat( wordindex, buffer);
}
// print binary string.
printf("%s\n", wordindex);
}
void main(void)
{
sample();
system("PAUSE");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment