NOTE: You need to create two more files as well, in.c and out.c. The in.c file is used to give input and output is obtained at out.c.
#include
<stdio.h>
#include
<string.h>
#include
<stdlib.h>
#include
<ctype.h
char
id[10][10],val[10][10],c;
FILE
*ifp,*ofp;
int
defs=0,j;
int
getword(char w[])
{
int
l=0;
c=fgetc(ifp);
while(isalnum(c)||c==‘#’||(ispunct(c)==‘<’))
{
w[l++]=c;
c=fgetc(ifp);
}
w[l]=‘\0’;
return
l;
}
int
ismacro(char w[])
{
int
i;
for(i=0;i<defs;i++)
return
i;
return
-1;
}
main()
{
char
word[20];
int
i,l;
ifp=fopen(“in.c”,“r”);
ofp=fopen(“out.c”,“w”);
while(!feof(ifp))
{
if(strcmp(word,‘#define’)==0)
{
getword(word);
strcpy(id[defs],word);
getword(word);
strcpy(val[defs],word);
defs++;
}
}
printf(“\nDefinition\n”);
for(i=0;i<defs;i++)
printf(“%s\t%s\n”,id[i],val[i]);
rrewind(ifp);
while(!feof(ifp))
{
l=getword(word);
if(strcmp(word,“#define”)==0)
{
fprintf(ofp,“%s”,word);
printf(“%s”,word);
while((c=fgetc(idp))!=‘\n’)
{
fprintf(ofp,“%c”,c);
priintf(“%c”,c);
}
}
else
if(l!=0)
if((j=ismacro(word))!=-1)
{
fprintf(ofp,“%s”,val[j]);
printf(“%s”,val[j]);
}
else
{
fprintf(ofp,“%s”,word);
printf(“%s”,word);
}
fprintf(ofp,“%c”,c);
printf(“%c”,c);
}
fclose(ifp);
fclose(ofp);
}
0 comments:
Post a Comment