Implement a Simple Shell Command Parser

Code

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

void parse_shell_input(char *line) {
    // Your logic here
    char token[100][100]={0};
    int i=0,count=0,index1=0,index2=0;
    while(line[i]!='\0')
    {
        if(line[i]!=' ')
        {
            token[index1][index2]=line[i];
            index2++;
        }
        if(line[i]==' ' && line[i+1]!=' ')
        {
            token[index1][index2]='\0';
            index1++;
            index2=0;
            count++;
        }
        i++;
    }
    token[index1][index2]='\0';
    count++;
    for(int i=0;i<count;i++)
    {
        printf("%s\n" ,token[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