All submissions

Implement a Simple Shell Command Parser

Code

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

void parse_shell_input(char *line) {
   
    uint8_t token_counter = 0;

    uint8_t char_counter = 0;

    uint8_t space_detected = 0;

    char commands[128][128];

    uint8_t char_detected = 0;

    memset(commands,'\0',sizeof(commands));

    for(int i=0; line[i]!='\0'; i++)
    {
        if(line[i]==' ' && char_detected)
        {
            space_detected = 1;
        }

        else if(isdigit(line[i]) || isalpha(line[i]))
        {
            if(space_detected == 1)
            {
                commands[token_counter][char_counter] = '\0';
                token_counter++;
                char_counter = 0;
                space_detected = 0;
            }

            char_detected = 1;
            commands[token_counter][char_counter] = line[i];
            char_counter++;
        }
    }

    commands[token_counter++][char_counter] = '\0';

    for(int i=0; i<token_counter; i++)
    {
        printf("%s\n",commands[i]);
    }
}

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

    parse_shell_input(line);
    return 0;
}

Solving Approach

 

 

 

Loading...

Input

led set 3 on

Expected Output

led set 3 on