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][20];   
    int row = 0, col = 0, i = 0;
    int in_token = 0;

    while (line[i] != '\0') {
        if (line[i] == ' ') {
            if (in_token) {
             
                tokens[row][col] = '\0';
                row++;
                col = 0;
                in_token = 0;
            }
        } else {

            in_token = 1;
            if (col < 19 && row < 10)
                tokens[row][col++] = line[i];
        }
        i++;
    }

    if (in_token) {
        tokens[row][col] = '\0';
        row++;
    }

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

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