103. Implement a Simple Shell Command Parser

Back To All Submissions
Previous Submission
Next Submission

Code

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

void parse_shell_input(char *line) {
    // Your logic here
    char *myLine = line;
    int myIndex = 0;
    int count = 0;
    int myTokens = 0;
    char tokens[4][10];

    while(*myLine != 0x00){     //get string length
        count++;
        myLine++;
    }

    for (int i = 0; i < count; i++){
        
        while((*line != 0x00) && (*line != ' ')){
            tokens[myTokens][myIndex++] = *line;
            line++;              
        }

        if(*line == ' ') {
            while(*line == ' ') line++;
        }
        tokens[myTokens++][myIndex] = 0x00;
        myIndex = 0;        
    }

    printf("%s\n", tokens[0]);
    printf("%s\n", tokens[1]);
    printf("%s\n", tokens[2]);
    printf("%s\n", tokens[3]);
}

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

    parse_shell_input(line);
    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote