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) {
    int cur_index=0;
    //1. Loop through each char
    while(line[cur_index]!='\0'){
        //2. If string contains letters (non spaces) print them
        if(line[cur_index]!=' '){
            printf("%c",line[cur_index]);
        }
        else{
            //3. Print a new line whenever you're at the end of the blank spaces
            if(line[cur_index]==' ' && line[cur_index+1] != ' '){
                printf("\n");
            }
        }
        cur_index++;
    }
}

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

    parse_shell_input(line);
    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote