Implement a Simple Shell Command Parser

Code

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

void parse_shell_input(char *line) {
    // Your logic here
    char *c = line;
    int count = 0;
    while(*c != '\0' && count < 100) {
        // move to the first char
        while((*c == 32) && (count < 100)) {
            c++;
            count++;
        }

        // count check
        if(*c == '\0' || count == 100){
            return;
        }

        char buff[101];
        buff[101] = '\0';
        int buff_idx = 0;
        while((*c != 32) && (count < 100) && (*c != '\0')) {
            buff[buff_idx++] = *c++;
            count++;
        }
        buff[buff_idx] = '\0';

        // print value
        printf("%s\n", buff);
    }
}

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