GoHacking.com: Ethical Hacking and Cyber Security
Home » C PROGRAMMING

C Program to Remove Comments and Blank lines from a valid C Program

Submitted by on Saturday, 24 November 200710 Comments

This program accepts any valid C Program as an input and removes the comments and blanks from it. The output is a program that has no comments and blank lines.

#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<dos.h>
void main()
{
FILE *a,*b;
char fname[20],ch,tch=NULL,tch1=NULL;
int flag1=0,flag=0,count=0,count1=0,count2=0;
clrscr();
printf(“Enter the file name (.C or .TXT)\n”);
gets(fname);
a=fopen(fname,”r”);
if(a==NULL)
{
puts(“Cannot open the source file!!!”);
delay(500);
exit(1);
}
b=fopen(“target.c”,”w”);
if(b==NULL)
{
puts(“Cannot create target file!!!”);
delay(500);
exit(1);
}
while(1)
{
ch=fgetc(a);
if(ch==EOF) break;
else
{
if(ch==’\n’)
{
count1=1;
tch1=ch;
continue;
}
else if(ch==’\n’&& count1==1)
continue;
else if(ch==’/'&&count==0)
{
flag1=1;
tch=ch;
count=1;
continue;
}
else if(ch==’*'&& flag1==1)
{
flag=1;
flag1=0;
tch=NULL;
}
else if(ch==’*'&&flag==1)
{
count2=1;
count=1;
continue;
}
else if(ch==’/'&&count2==1)
{
flag=0;
count=0;
tch=NULL;
continue;
}
else if(count==1&&flag==1)
count=0;
else if(flag1==1)
flag1=0;
}
if(flag!=1)
{
if(tch>0)
fputc(tch,b);
if(tch1>0)
fputc(tch1,b);
tch1=NULL;
tch=NULL;
count=0,count1=0,count2=0;
fputc(ch,b);
flag1=0,flag=0;
}
}
puts(“DONE!! OP FILE IS \”TARGET.C\”\n”);
fcloseall();
getch();
}



By using/following this site you agree to our Legal Disclaimer

10 Comments »

  • Abhijeet said:

    Hi!I am browsing this web site from last two month , i want to suggest one thing that new information should be added every week because computer field has huge and interesting knowledge.

  • Karthikeyan said:

    i am using this website for last two days . This website is very interesting.

  • Srikanth (author) said:

    thanks for your suggestion.. Will try to update my blog freequently.

  • KISHORE said:

    HEY man you are great, how do you know all these things…………???????

  • abhi said:

    HE i want borland c++ compiler 5.5 for free
    Plz tell me the exact link where i can get it

    I hve tried codegear website but i m not able 2 find where i can can it for free!!!

    kindly help!!!

  • abhi said:

    Thanx

  • abhi said:

    I want borland c++ compiler 5.5
    i hve turbo c++ but it is a 16 bit compiler
    i want to change my software’s icon so can u tell me the exact URL for that???
    plz help me I have tried codegear.com but i m not able 2 find it for free!!!
    help me!!

  • Sandeep Kumbhar said:

    Hey hi shri…
    I learned many things cos of your web site buddy…kindly thanks a lot…!!!

  • Rashmi said:

    The info provided by u is awesome n damn helpful.
    Thanks a lot… :)

  • raju said:

    very big program but i will try

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.