Problem Description: Ram's first job is a out of university is for a student's book publisher. They are currently publishing a line of "intelligent libs" books, and they need you to check their print-copy. Write a program that will take a sentence with placeholders in it, and substitute in for those placeholders words from the lists of example words the publisher has provided. Your goal will be to substitute the appropriate words into the sentence from the list of provided words TWICE (keeping track of the words you have used) where: [N] = nouns [AV] = adverbs [V] = verbs [AJ] = adjectives
C Program


#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#define CMDS 5

#define TOKENS 4

#define MAXWORDS 50

#define MAXLINE 512

char *tokens[TOKENS]={"[N]","[AV]","[V]","[AJ]"};

char *cmds[CMDS]={"NOUNS","ADVERBS","VERBS","ADJECTIVES","END"};

char *lists[CMDS][MAXWORDS];

int cl[CMDS];

int ci[CMDS];

int lookup(char *p,int *l){

    int t;

    for(t=0;t<TOKENS;t++) if(!strncmp(p,tokens[t],*l=strlen(tokens[t]))) return t;

    return -1;

}

void substitute(char *s){

    int t,l;char r[MAXLINE]="",*p=s;

    while(*p){

        if((t=lookup(p,&l))>=0){strcat(r,lists[t][ci[t]++]);p+=l;}

        else strncat(r,p++,1);

    }

    puts(r);

}

int getcmd(char *s){

    int i;

    for(i=0;i<CMDS;i++) if(!strcmp(s,cmds[i])) return i;

    return -1;

}

int main(){

    char line[MAXLINE],story[MAXLINE];

    int cur=0;

    fgets(story,MAXLINE,stdin);

    story[strcspn(story,"\n")]=0;

    while(fgets(line,MAXLINE,stdin)){

        line[strcspn(line,"\n")]=0;

        int c=getcmd(line);

        if(c>=0) cur=c; else lists[cur][cl[cur]++]=strdup(line);

    }

    substitute(story);substitute(story);

    return 0;

}