Implement a Simple Shell Command Parser

Code

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

void parse_shell_input(char *line) {
    // Your logic here
    int index = 0;
    int col = 0; 
    int row = 0; 
    char tokens[10][10]; 
    bool newToken = true; 

    while(line[index] != '\0')
    {   
        if(line[index] == ' ')
        {   
            tokens[row][col] = '\0';
            if(newToken == true)
            {
                printf("%s\n", tokens[row]); 
                row++; 
                col = 0;
                newToken = false; 
            }
            index++; 
            continue; 
        }
        newToken = true; 
        tokens[row][col] = line[index]; 
        index++;
        col++; 
    }
    tokens[row][col] = '\0';
    printf("%s", tokens[row]);
}

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