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...");
}

4 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

Related Posts Plugin for WordPress, Blogger...