Implement a Simple Shell Command Parser

Code

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

void parse_shell_input(char *line) {
    // Your logic here
    char words[10][10];
    int cnt=0,i,j=0,spcnt=0;
    for(i=0;line[i]!='\0';i++)
    {
         if(line[i]==' ')
         {
            spcnt++;
         }
            
         
         else
         {
            if(spcnt>0)
            {
                words[cnt][j]='\0';
            cnt++;
            j=0;
            spcnt=0;
            }
            words[cnt][j]=line[i];
            j++;
         }
    }
    
    words[cnt][j]='\0';
            cnt++;
            for(int i=0;i<cnt;i++)
            {
                printf("%s\n",words[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