Question
ou need to write a C function named parse. The prototype is: int parse(char *); The function accepts a string; scans it with sscanf to
ou need to write a C function named parse. The prototype is:
int parse(char *);
The function accepts a string; scans it with sscanf to try to convert it to an integer; and returns a value as follows:
if the integer=1, return 1 if the integer=2, return 2 if the integer=3, return 3 in all other cases, return -1
This means if the string is empty, if it starts with a non-digit, if it's an integer bigger than 3 or less than 1, if it has non-printabe characters, etc. then your function should return -1. Use sscanf to do the conversion. If sscanf can convert it to an integer, use that integer value and make sure it's between 1 and 3. If sscanf can't convert it to an integer, return -1.
Write your own main test function to test your parse function. Test it however you see fit: you can hardwire values, input values while your program runs, use command line parameters, of whatever else makes sense to you.
If your main function is called main.c compile as follows:
gcc -o main main.c parse.c
Test your program thoroughly. Then, once you are convinced that it works, assess it as follows:
put your function in a file named parse.c There should not be a main function in parse.c: it should only contain your function.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started