Implement a Simple Shell Command Parser

Code

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

void parse_shell_input(char *line) {
    // Your logic here
    char *start = line; //start pointer set to starting of line

    while(*line){
        if(*line == ' '){
            *line = '\0';
            if(*start != '\0')
                printf("%s\n", start);
            start = line + 1;
        }
        line++;
    }

    *line = '\0'; //last token
    printf("%s", start);
}

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

    parse_shell_input(line);
    return 0;
}

Solving Approach

Iniitalize a start pointer pointing to the starting character address of the string.

Loop thorugh the input string, each time a space is encountered

  • replace the space with a null character
  • Place start pointer 1 place after the loop pointer

 

 

 

Upvote
Downvote
Loading...

Input

led set 3 on

Expected Output

led set 3 on