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 words[100][100];
    int n=strlen(line);
    int i=0;
    int j=0;
    char prev='\0';
    
    for(int x=0;x<n;x++)
    {
        if(line[x]==' '&& prev!=' ')
        {
            i++;
            j=0;
        }
        else if(line[x]!=' ')
        {
            words[i][j++]=line[x];
        }
        prev=line[x];
    }
    for(int k=0;k<=i;k++)
    {
        printf("%s\n",words[k]);
    }
}

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