103. Implement a Simple Shell Command Parser

Back To All Submissions
Previous Submission
Next Submission

Code

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

void parse_shell_input(char *line) {
   char token[10]={0};
   char *ptr = token;
   while(*line){
    if(*line==' '){
        if(ptr!=token)
            printf("%s\n",token);
        memset(token,0,sizeof(token));
        ptr = token;
       
    }
    if(*line!=' ')
        *(ptr++)=*line;
    line++;
    
   }
    printf("%s",token);
}

int main() {
    char line[101];
    fgets(line, sizeof(line), stdin);

    parse_shell_input(line);
    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote