Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to convert the string argument to an integer using atoi.
C
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { if (argc < 2) { printf("Please provide a number.\n"); return 1; } int num = [1](argv[1]); printf("Number is %d\n", num); return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using printf instead of atoi.
Using strlen which returns string length, not number.
Using scanf incorrectly here.
✗ Incorrect
The function atoi converts a string to an integer. Here, argv[1] is the string argument to convert.
2fill in blank
mediumComplete the code to parse a long integer from the string argument using strtol.
C
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { if (argc < 2) { printf("Please provide a number.\n"); return 1; } char *endptr; long val = [1](argv[1], &endptr, 10); if (*endptr != '\0') { printf("Invalid number: %s\n", argv[1]); return 1; } printf("Parsed long: %ld\n", val); return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using atoi which does not detect invalid characters.
Using strcpy which copies strings but does not convert.
Using strlen which returns length, not number.
✗ Incorrect
strtol converts a string to a long integer and allows error checking via endptr.
3fill in blank
hardFix the error in the code to correctly parse a double from the string argument using strtod.
C
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { if (argc < 2) { printf("Please provide a number.\n"); return 1; } char *endptr; double val = [1](argv[1], &endptr); if (*endptr != '\0') { printf("Invalid number: %s\n", argv[1]); return 1; } printf("Parsed double: %f\n", val); return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using atof which does not allow error checking.
Using atoi which converts to int, not double.
Using strtol which converts to long, not double.
✗ Incorrect
strtod converts a string to a double and allows error checking via endptr.
4fill in blank
hardFill both blanks to create a dictionary-like structure that maps argument index to its parsed integer value if the argument is numeric.
C
#include <stdio.h> #include <stdlib.h> #include <ctype.h> int is_number(const char *s) { while (*s) { if () return 0; s++; } return 1; } int main(int argc, char *argv[]) { for (int i = 1; i < argc; i++) { if (is_number(argv[i])) { int val = [1](argv[i]); printf("Arg %d is number %d\n", i, val); } else { printf("Arg %d is not a number\n", i); } } return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using strtol without proper error checking.
Using printf instead of conversion function.
Using isdigit on whole string instead of characters.
✗ Incorrect
atoi converts string to int; isdigit checks if a character is a digit.
5fill in blank
hardFill all three blanks to parse command line arguments as integers, check for errors, and print the sum.
C
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int sum = 0; for (int i = 1; i < argc; i++) { char *endptr; int val = (int)[1](argv[i], &[2], 10); if (*[3] != '\0') { printf("Invalid number: %s\n", argv[i]); return 1; } sum += val; } printf("Sum is %d\n", sum); return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using atoi which does not allow error checking.
Not checking endptr to detect invalid input.
Confusing variable names for endptr.
✗ Incorrect
strtol parses string to long with error checking via endptr pointer.