103. Implement a Simple Shell Command Parser

Back To All Submissions
Previous Submission
Next Submission

Code

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

void parse_shell_input(char *line) {
    // Your logic here
    char token[10][10]={0};
    int i=0; 
    int row=0; 
    int col=0;

    while(line[i] != '\0'){
        if(line[i]==' '){
            if(col>0){
                token[row][col]='\0';
                row++;
                col=0;
            }
        }else{
            token[row][col]=line[i];
            col++;
        }
        i++;
    }
    token[row][col]= '\0';
    for(int j=0; j<row+1; j++){
        printf("%s\n", token[j]);
    }
}

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

    parse_shell_input(line);
    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote