Implement a Simple Shell Command Parser

Code

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

void parse_shell_input(char *line) {
    // Your logic here
    char strArray[10][101];
    int i = 0;
    int tokenIndex = 0;
    int arrayIndex = 0;

    
    while(line[i] != '\0' && arrayIndex < 10){
        if(line[i] == ' '){
            if(tokenIndex > 0){
                strArray[arrayIndex][tokenIndex] = '\0';
                arrayIndex++;
                tokenIndex = 0;
            }
        } else {
            if(tokenIndex < 100){
                strArray[arrayIndex][tokenIndex] = line[i];
                tokenIndex++;
            }
        }
        i++;
    }
    if (tokenIndex > 0 && arrayIndex < 10)
    {
        strArray[arrayIndex][tokenIndex] = '\0';
        arrayIndex++;
    }
    for(int j = 0; j < arrayIndex; j++){
        if(strArray[j] != NULL){
            printf("%s\n", strArray[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