Implement a Simple Shell Command Parser

Code

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

void parse_shell_input(char *line) {
    // Your logic here
    char tokens[10][10];
    int row = 0;
    int col = 0;

    while (1) {

        if ((*line == ' ' && *(line-1) != ' ') || (*line == 0)) {
            tokens[row][col] = 0;
            row++;
            col = 0;

            if (*line == 0) {
                break;
            }
        } else if (*line == ' ' && *(line-1) == ' ') {
            // do nothing
        } else {
            tokens[row][col++] = *line;
        }
        line++;
    }

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

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

    parse_shell_input(line);
    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

led set 3 on

Expected Output

led set 3 on