Implement a Simple Shell Command Parser

Code

#include <stdio.h>
#include <ctype.h>
void parse_shell_input(char *line)
{
    int i = 0;
    while (line[i] == ' ')
        ++i;

    int in_space = 0;

    while (line[i] != '\0')
    {
        if (line[i] == ' ')
        {
            if (!in_space)
            {
                printf("\n");
                in_space = 1;
            }
        }
        else
        {
            printf("%c", line[i]);
            in_space = 0;
        }
        ++i;
    }
}

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