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][20] = {0};
    int ROW=0,COL=0;
    for(int i=0; line[i]!='\0';i++)
    {
        if(line[i]==' ')
        {
            if(line[i+1]==' ')
            {
                continue;
            }
            else
            {
                token[ROW++][++COL]='\0';
                COL=0;
                continue;
            
            }
        }   
        else
        {
            token[ROW][COL]=line[i];
            COL++;
        }
    }
    for(int j=0;j<=ROW;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