1. Engineering
2. Computer Science
3. instructions write a program bindecc that will take a binary...

# Question: instructions write a program bindecc that will take a binary...

###### Question details

INSTRUCTIONS: Write a program bindec.c that will take a binary numbers as input, and output the decimal equivalent. Look for "YOUR CODE HERE". Modify bindec.c so that it converts the input string into a number, and prints the number. Write the code to convert from binary to decimal yourself (don’t use a library call). Your code should respond to empty input with value 0, and should respond to invalid input with message ‘input must contain only zeros and ones’, and exit with value 1, indicating an error. Your code should be able to handle inputs of at least 20 binary digits.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// max length input string
#define MAXSTR 25

// convert input binary string to a number

int main() {

// user input string
char s[MAXSTR+3];

// prompt for input
printf("> ");

// read input string; at most MAXSTR+1 chars accepted
// Note: this is tricky. If we accept only MAXSTR chars,
// we can't see if user entered more chars and they are
// being dropped by fgets.
fgets(s, MAXSTR+3, stdin);

// check input length; n does not include final carriage return
int n = strlen(s)-1;
if (n > MAXSTR) {
printf("input cannot be more than %d characters\n", MAXSTR);
exit(1);
}

// convert s from a string in binary, to an int, and output

// YOUR CODE HERE

}

###### Solution by an expert tutor 