Implement a Simple Shell Command Parser

Code

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

void parse_shell_input(char *line) {

    char word[101];
    int i = 0;
    int j = 0;
    while(line[i] != '\0'){
        if (line[i] != ' '){
            word[j] = line[i];
            j++;
        }
        else{
            while (line[i+1] == ' '){i++;}
            word[j] = '\0';
            printf("%s\n", word);
            j=0;
        }
        i++;
    }

    word[j] = '\0';
    printf("%s\n", word);
}

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