All submissions

Implement a Simple Shell Command Parser

Code

#include <stdio.h>
#include <ctype.h>

void parse_shell_input(char *line) {
    int i = 0;

    while (line[i] != '\0') {
        // skip spaces and tabs
        while (line[i] != '\0' && isspace((unsigned char)line[i])) {
            i++;
        }

        if (line[i] == '\0' || line[i] == '\n')
            break;

        // start of a token
        while (line[i] != '\0' && !isspace((unsigned char)line[i])) {
            putchar(line[i]);
            i++;
        }

        // end of token
        putchar('\n');
    }
}

int main() {
    char line[101];
    if (fgets(line, sizeof(line), stdin) != NULL) {
        parse_shell_input(line);
    }
    return 0;
}

Solving Approach

 

 

 

Loading...

Input

led set 3 on

Expected Output

led set 3 on