Implement a Simple Shell Command Parser

Code

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

void parse_shell_input(char *line) {
    // Your logic here
    unsigned char len = 0;
    char command[100];
    unsigned char pos = 0;
    unsigned char print = 0;

    while(*(line + len) != '\0')
    {
        if(*(line + len) != ' ')
        {
            command[pos++] = *(line + len);
            print = 0;
        }
        else
        {
            command[pos] = '\0';
            if (print == 0)
            {
                printf("%s\n", command);
                print = 1;
            }
            pos = 0;
        }

        len += 1;
    }

     command[pos] = '\0';
    printf("%s", command);

}

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