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;
}
0 Comments