Comment Removal Program - Compiler Lab - C Program

Program:

#include<stdio.h>
main()
{
    FILE *ip,*op;
    char ipname[10],opname[10];
    char c;
    int flag=0;
    printf("\nEnter the input file name : ");
    scanf("%s",ipname);
    printf("\nEnter the output file name : ");
    scanf("%s",opname);
    if((ip=fopen(ipname,"r"))==NULL)
        printf("Input file does not exist !\n");
    else
    {   
        op=fopen(opname,"w");
        while((c=getc(ip))!=EOF)
        {
            if(c=='/')
            {
                c=getc(ip);
                if(c=='*')
                    flag=1;
                if(c=='/')
                    flag=2;
            }
   
            if((flag==2)&&(c=='\n'))
                flag=0;
            if(flag==0)
                putc(c,op);
            if((flag==1)&&(c=='*'))
            {
                c=getc(ip);
                if(c=='/')
                    flag=0;
            }
        }
        fclose(ip);
        fclose(op);
    }
}


Output:

nn@ubuntu:~$ gcc cmt.c
nn@ubuntu:~$ ./a.out

Enter the input file name : a.c

Enter the output file name : b.c
nn@ubuntu:~$

a.c: (Input File)
#include<stdio.h>
main()
{
prntf("hai...");//comment removal program in c
printf("hello...");/*www.2k8618.blogspot.com */
}


b.c:(Output File)
#include<stdio.h>
main()
{
prntf("hai...");
printf("hello...");
}

5 comments:

  1. if(c=='*') this statement(line 32) should be changed to if((c=='*')&&(flag==1)) for the perfect output

    ReplyDelete
  2. Programming is very interesting and creative thing if you do it with love. Your blog code helps a lot to beginners to learn programming from basic to advance level. I really love this blog because I learn a lot from here and this process is still continuing.
    Love from Pro Programmer

    ReplyDelete

  3. I appreciate that you produced this wonderful article to help us get more knowledge about this topic.
    I know, it is not an easy task to write such a big article in one day, I've tried that and I've failed. But, here you are, trying the big task and finishing it off and getting good comments and ratings. That is one hell of a job done!

    Selenium training in bangalore
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training
    Selenium interview questions and answers

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...