Write a program to remove the C comments(/* */) and C++
comments(//) from a file.
The file should be declared in command line.

Answers were Sorted based on User's Feedback



Write a program to remove the C comments(/* */) and C++ comments(//) from a file. The file should ..

Answer / 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

Write a program to remove the C comments(/* */) and C++ comments(//) from a file. The file should ..

Answer / palash das

//This works on c-string...


#include <stdio.h>

void remove_cmmnt(char *s)
{
int i,j;
for(i=j=0; s[j] ; )
{
if(s[j]=='/' && s[j+1] && s[j+1]=='/')
for(j+=2; s[j] && s[j++]!='
'; ) ;
else if(s[j]=='/' && s[j+1] && s[j+1]=='*')
for(j+=2; s[j] && s[++j] && (s[j-1]!='*' || s[j]!='/' || !j++); );
else
s[i++]=s[j++];
}
s[i]='';
}

int main()
{
char s[]="/*123***/Hello // Cross
World /* **NachLeCoders";
remove_cmmnt(s);
puts(s);
return 0;
}

Is This Answer Correct ?    2 Yes 4 No

Write a program to remove the C comments(/* */) and C++ comments(//) from a file. The file should ..

Answer / sahil

/*Improved and working answer*/
#include<stdio.h>
void main()
{
FILE *fd;
int ch,flag,i;
char cc;
i=flag=0;
ch=0;
fd = fopen("file","r+");
// ch=fgetc(fd);
while((ch=fgetc(fd))!=EOF)
{ cc=ch;
flag = 0;
if(ch == '/')
{
ch = fgetc(fd);cc =ch;
if(ch == '*')
{
flag = 1;
while(1){
cc = fgetc(fd);
if(fgetc(fd) == '*' && fgetc(fd) == '/')
break;
}
}
else if(ch == '/')
{
flag = 1;
while(fgetc(fd)!= '/');
}

else{
printf("/");
}
}
if(!flag )
printf("%c",ch);
}
fclose(fd);
}

Is This Answer Correct ?    9 Yes 13 No

Write a program to remove the C comments(/* */) and C++ comments(//) from a file. The file should ..

Answer / abdur rab

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int get_file_size ( const char* file_name )
{
struct stat _file_info;
int _n_bytes = 0;

if ( 0 <= stat ( file_name, &_file_info ) ) {
if ( S_ISREG ( _file_info.st_mode ) )
_n_bytes = _file_info.st_size;
}

return ( _n_bytes );
}

char* read_content ( const char* file_name )
{
FILE* _file_pointer = NULL;
char* cp_file_content = NULL;
int _n_bytes = 0;

_n_bytes = get_file_size ( file_name );
cp_file_content = (char*) malloc ( ( _n_bytes + 1 )
* sizeof ( char ) );
if ( NULL != cp_file_content ) {
_file_pointer = fopen ( file_name, "r" );
if ( _file_pointer ) {
if ( _n_bytes != fread (
cp_file_content, 1, _n_bytes, _file_pointer ) ) {
printf ( "\n The File
Name :%s, Read Problem", file_name );
free ( cp_file_content );
cp_file_content = NULL;
} else {
cp_file_content [
_n_bytes ] = '\0';
}
fclose ( _file_pointer );
}
}

return ( cp_file_content );
}

int main ( int argc, char* argv [] )
{
char* cp_file_content = NULL;
int n_length = 0;
int n_counter = 0;

if ( argc <= 1 || argc > 3 ) {
printf ( "\n Usage : comment_remover
<file_name>" );
exit ( 0 );
}

cp_file_content = read_content ( argv [ 1 ] );

if ( NULL != cp_file_content ) {
n_length = strlen ( cp_file_content );
for ( n_counter = 0; n_counter < n_length;
n_counter++ ) {
if ( ( * ( cp_file_content +
n_counter ) == '/' )
&& ( * (
cp_file_content + n_counter + 1 ) == '*' ) ) {
while ( * ( cp_file_content
+ n_counter ) != '\0' ) {
n_counter++;
if ( ( * (
cp_file_content + ( n_counter - 1 ) ) == '*' )
&&
( * ( cp_file_content + n_counter ) == '/' ) ) {

n_counter++; // move away from / (slash)
break;
}
}
} else if ( ( * ( cp_file_content +
n_counter ) == '/' )
&& ( * (
cp_file_content + n_counter + 1 ) == '/' ) ) {
while ( * ( cp_file_content
+ n_counter ) != '\n' ) n_counter++;
}
printf ( "%c", * ( cp_file_content
+ n_counter ) );
}
}
}

Is This Answer Correct ?    10 Yes 23 No

Post New Answer

More C Interview Questions

Explain this code. #include <stdio.h> void f1(int *k) { *k = *k + 10; } main ( ){ int i; i = 0; printf (" The value of i before call %d \n", i); f1 (&i); printf (" The value of i after call %d \n", i); }

3 Answers   IBM,


How does C++ help with the tradeoff of safety vs. usability?

1 Answers  


code for find determinent of amatrix

0 Answers  


Explain the difference between structs and unions in c?

0 Answers  


How to set file pointer to beginning c?

0 Answers  






when i declare as: void main() { clrscr(); int a=10; printf("%d",a) } my problem that why generate a error in above programs. please tell me answer seriously .

4 Answers  


What is the difference between memcpy and memmove?

0 Answers  


Consider the following C program. #include <stdio.h> int main() { int i; for (i=0;i<3;++i) { fork();fork(); } } How many processes are created when running this program (including the initial one)? Explain

2 Answers  


2. Write a function called hms_to_secs() that takes three int values&#8212;for hours, minutes, and seconds&#8212;as arguments, and returns the equivalent time in seconds.. Create a program that exercises this function by repeatedly obtaining a time value in hours, minutes, and seconds from the user (format 12:59:59), calling the function, and displaying the value of seconds it returns.

5 Answers   TCS,


How can you check to see whether a symbol is defined?

0 Answers  


What is the meaning of this decleration? unsigned char (*pArray[10][10]); please reply.

1 Answers  


What are run-time errors?

0 Answers  


Categories