0
0
Cprogramming~10 mins

Parsing numeric arguments - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Aatoi
Bstrlen
Cprintf
Dscanf
Attempts:
3 left
💡 Hint
Common Mistakes
Using printf instead of atoi.
Using strlen which returns string length, not number.
Using scanf incorrectly here.
2fill in blank
medium

Complete 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'
Astrcpy
Bstrlen
Catoi
Dstrtol
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.
3fill in blank
hard

Fix 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'
Astrtol
Bstrtod
Catoi
Datof
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.
4fill in blank
hard

Fill 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 (![2](*s)) 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'
Aatoi
Bstrtol
Cisdigit
Dprintf
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.
5fill in blank
hard

Fill 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'
Astrtol
Bendptr
Datoi
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.