Implement a Simple Shell Command Parser

Code

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

void parse_shell_input(char *line) {
    // Your logic here
    char tokens[50][50];
    int count = 0, idx = 0;
    while (*line != '\0') {
        if (*line == ' ' && idx != 0) {
            tokens[count++][idx] = '\0';
            idx = 0;
        } else if (*line != ' ') {
            tokens[count][idx++] = *line;
        }
        line++;
    }
    tokens[count++][idx] = '\0';
    for (int i = 0; i < count; 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