Saturday, 8 September 2018

Write a C/C++ program to identify whether a given line is comment or not.GTU Complier Design 2170701


               Code :

               #include<stdio.h>
               #include<stdlib.h>
               int main()
               {
                    FILE *f1,*f2;
                    f1=fopen("input.c","r");
                    f2=fopen("output.txt","w");
                    char ch=fgetc(f1);
                    int count=1;
                    while(ch!=EOF)
                    {
                                if(ch=='\n')
                                {
                                            count++;
                                }
                                if(ch=='/')
                                {
                                            ch=fgetc(f1);
                                            if(ch=='/')
                                            {
                                                        fprintf(f2,"\nSingle line comment at line %d",count);
                                            }
                                            else if(ch=='*')
                                            {
                                                        fprintf(f2,"\nMultiple line comment start at line %d",count);
                                                        ch=fgetc(f1);
                                                        while(ch!='*')
                                                        {
                                                                    if(ch=='\n')
                                                                    {
                                                                                count++;
                                                                    }
                                                                    ch=fgetc(f1);
                                                        }
                                                        if(ch=='*')
                                                        {
                                                                    ch=fgetc(f1);
                                                                    if(ch=='/')
                                                                    {
                                                                                fprintf(f2," and end at line %d",count);
                                                                    }
                                                        }
                                            }
                                }
                                ch=fgetc(f1);
                    }
                    printf("Check the output in outpt.txt");
                    return 0;
               }

No comments:

Post a Comment

It's time To increase blogging capability. To have a chance to contribute in digital world. Any Interested People who want to make t...