Implement a Simple Shell Command Parser

Code

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

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