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

How do we open a binary file in Read/Write mode in C?

0 Answers   Alter,


Go through the following code sinippet char a[20]; a="Hello Orcale Test"; will this compile?

4 Answers   Oracle,


How to write a code for reverse of string without using string functions?

0 Answers   TCS,


How to write a program for machine which is connected with server for that server automatically wants to catch the time for user of that machine?

0 Answers   Infosys,


how can i make a program with this kind of output.. Enter a number: 5 0 01 012 0123 01234 012345 01234 0123 012 01 0

4 Answers   Wipro,






what is the c.

3 Answers   IBM, TCS,


Write a code of a general series where the next element is the sum of last k terms.

0 Answers   Aspiring Minds,


can i know the source code for reversing a linked list with out using a temporary variable?

6 Answers   Honeywell,


How the processor registers can be used in C ?

7 Answers   HP,


Write a program in C to print the alphabets in order as on a mobile phone.i.e:When 2 is pressed once 'a' prints and if it is pressed two times 'b' prints and so on.we have to print all the alphabets as on mobile phone like this.

1 Answers   Wipro,


Is it possible to execute code even after the program exits the main() function?

0 Answers  


what is level of tree if leaf node is at level 4.please explain.

1 Answers   Wipro,


Categories