Implement a Simple Shell Command Parser

Code

#include <stdio.h>
#include <ctype.h>
#include<string.h>
void parse_shell_input(char *line) {
    // Your logic here
    char arr[100][100];
    int rowIndex=0;
    int colIndex=0;
    int totalcount=0;
    while(*line)
    {
        if((*line  == ' ' || *line == '\n') )
        {
            if(colIndex>0)
            {
                arr[rowIndex][colIndex]='\0';
                rowIndex++;
                colIndex=0;
            }
        }
        else
        {
            arr[rowIndex][colIndex++]=*line;
        }
        line++;
    }
    if (colIndex > 0) {
        arr[rowIndex][colIndex] = '\0';
        rowIndex++;
    }
    for (int i = 0; i < rowIndex; i++) {
        printf("%s\n", arr[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