Implement a Simple Shell Command Parser

Code

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

void parse_shell_input(char *line) {
    // Your logic here
    char* current_word_start = line;
    bool sp_det_flag = false;
    while(*line!='\0'){
        if(*line==' '){
            //space char detected if this is the first one print word
            if(sp_det_flag==false){
                *line='\0';
                printf("%s\n", current_word_start);
                sp_det_flag = true;
            }
            else{
            }
        }
        else{
            // reset first space detection
            if(sp_det_flag){
                current_word_start = line;
                sp_det_flag = false;
            }
        }
        line++;
    }
    printf("%s\n", current_word_start);
}

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