-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringBinaryEquivalent.c
More file actions
47 lines (38 loc) · 908 Bytes
/
StringBinaryEquivalent.c
File metadata and controls
47 lines (38 loc) · 908 Bytes
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <string.h>
//The input function
void input(char str[]){
printf("Input a string: ");
fgets(str, 255, stdin);
}
//Converting the decimal number to a binary number
long int decToBin(int dec){
long int bin = 0;
int p = 1;
while(dec != 0){
bin += (dec%2)*p;
p *= 10;
dec /= 2;
}
return bin;
}
//The output function
void output(long int a[], int s){
printf("\nThe binary string equivalent is:\n");
for(int k = 0; k < s-1; k++){
printf("%ld ", a[k]);
}
}
int main(){
//Taking Input
char str[255];
input(str);
//Converting the string's characters to binary numbers
long int bin[strlen(str)];
for(int j = 0; j < strlen(str); j++){
bin[j] = decToBin((int)(str[j]));
}
//Printing the output
output(bin, strlen(str));
return 0;
}