Implement a Simple Shell Command Parser

Code

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

void parse_shell_input(char *line) {
    // Your logic here
    char token[50][20];
    int i = 0, t = 0, c = 0;

    while(line[i] != '\0'){
        if(line[i] == ' '){
            if(c>0){ /////executes ifff line[i] has a word
            token[t][c] = '\0';
            t++;
            c = 0;
            }

        }else{
            token[t][c] = line[i];
            c++;
        }
        i++;
    }
        if(c>0){
        token[t][c] = '\0';
        t++;
        }

        int count = t;

        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

 

 

 

Upvote
Downvote
Loading...

Input

led set 3 on

Expected Output

led set 3 on