All submissions

Convert Binary String to Integer Without strtol function or Libraries

Code

#include <stdio.h>
#include <stdint.h>
#include<string.h>
uint16_t binary_to_uint(const char *str) {
    // Your logic here
    int value=1,decimal=0;
    int n=strlen(str);
    while(n>=0){
        char y=*(str+n-1);
        if(y=='1'){
            decimal+=(value);
        }
        value*=2;
        n--;
    }
    return decimal;
}

int main() {
    char bin[20];
    scanf("%s", bin);

    printf("%u", binary_to_uint(bin));
    return 0;
}

Solving Approach

simple looping and bit manipulation to solve

 

 

Loading...

Input

1010

Expected Output

10