All submissions

Implement a Simple Shell Command Parser

Code

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

void parse_shell_input(char *line) {
    char tokens[50][101];
    char delimiter = ' ';

    int token_ctr = 0;
    int token_idx = 0;
    int line_idx = 0;

    while(line[line_idx] != '\0') {
        if (line[line_idx] == delimiter) {
            if (line[line_idx + 1] != delimiter) {
                tokens[token_ctr++][token_idx++] = '\0';
                token_idx = 0;
            }
        } else {
            tokens[token_ctr][token_idx++] = line[line_idx];
        }

        line_idx++;
    }

    tokens[token_ctr++][token_idx++] = '\0';

    for (int i=0; i<token_ctr; i++) {
        printf("%s\n", tokens[i]);
    }

}

int main() {
    char line[101];
    fgets(line, sizeof(line), stdin);

    parse_shell_input(line);
    return 0;
}

Solving Approach

 

 

 

Loading...

Input

led set 3 on

Expected Output

led set 3 on