103. Implement a Simple Shell Command Parser

Back To All Submissions
Previous Submission
Next Submission

Code

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

void parse_shell_input(char *line) {
    char token[10][20];
    uint8_t count = 0;
    uint8_t j = 0;
    while(*line) {
        if(*line == ' ' || *line == '\n') {
            if (j > 0) {
                token[count][j] = '\0';
                count++;
                j = 0;
            }
        } else {
            token[count][j] = *line;
            j++;
        }
        line++;
    }
    if (j > 0) {
        token[count][j] = '\0';
        count++;
    }
    for(int i=0; i<count; i++) {
        printf("%s\n", token[i]);
    }
}

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

    parse_shell_input(line);
    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote