Write a program to remove the C comments(/* */) and C++
comments(//) from a file.
The file should be declared in command line.
Answer Posted / vadivel
#include<stdio.h>
int main(int argc,char *argv[])
{
FILE *f;
int flag;
f = fopen(argv[1],"r");
while(!feof(f))
{
ch = fgetc(f),flag = 0;
if(ch == '/')
{
ch = fgetc(f);
if(ch == '*')
{
flag = 1;
while(1)
if(fgetc(f) == '*' && fgetc(f) == '/')
break;
}
else if(ch == '/')
{
flag = 1;
while(fgetc(f)!= '/n');
}
else
printf("/");// if it s division operator
}
if(!flag )
printf("%c",ch);
}
fclose(f);
}
/*
Run d prog as
>./a.out file_name.cpp
*/
| Is This Answer Correct ? | 54 Yes | 47 No |
Post New Answer View All Answers
How to create struct variables?
Is there sort function in c?
What is meant by gets in c?
Does c have enums?
How can this be legal c?
Explain what is wrong with this statement? Myname = ?robin?;
What is difference between Structure and Unions?
What is pointers in c with example?
Why c is called a middle level language?
What is a structure and why it is used?
What is meant by realloc()?
Add Two Numbers Without Using the Addition Operator
Explain function?
Is fortran still used today?
Why are all header files not declared in every c program?