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;   
    char token[101];
    int token_count = 0;
    while (line[i] != '\0')
    {
        if (line[i] == ' ')
        {
            // printf("\n");
            for (int j = 0 ; j < token_count ; j++)
            {
               printf("%c" , token[j]);
            }
            if (token_count != 0)
            {
                printf("\n");
            }
           
           
            for (int j = 0 ; j < token_count ; j++)
            {
                token[j] = '\0';
            }
            token_count = 0;

        }
        else 
        {
            token[token_count ++] = line[i];
        }
        i++;
    }
    //printf("\n");
    for (int j = 0 ; j < token_count ; j++)
            {
               printf("%c" , token[j]);
            }

}

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