Implement a Simple Shell Command Parser

Code

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

void parse_shell_input(char *line) {
    // Your logic here
    int i = 0;
    int in_word = 0;   // 0: đang ngoài từ, 1: đang trong từ

    while (line[i] != '\0') {

        // Nếu là chữ/số (không phải space)
        if (!isspace(line[i])) {
            putchar(line[i]);
            in_word = 1;
        }
        // Nếu là space
        else {
            if (in_word) {
                putchar('\n');   // kết thúc 1 token
                in_word = 0;
            }
        }

        i++;
    }

    // Nếu chuỗi kết thúc khi đang ở trong từ
    if (in_word) {
        putchar('\n');
    }
    
}

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