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

what is differnence b/w macro & functions

1 Answers  


write a program in c to print **** * * * * ****

1 Answers   TCS,


When reallocating memory if any other pointers point into the same piece of memory do you have to readjust these other pointers or do they get readjusted automatically?

0 Answers   TISL,


How can you find out how much memory is available?

0 Answers  


Explain what are linked list?

0 Answers  






What is the difference between realloc() and free()

1 Answers  


What is the relationship between pointers and data structure?

0 Answers  


what is difference between array and structure?

44 Answers   College School Exams Tests, CTS, Google, HCL, IBM, Motorola, TCS,


Magic square

0 Answers  


Write a C program linear.c that creates a sequence of processes with a given length. By sequence it is meant that each created process has exactly one child. Let's look at some example outputs for the program. Here the entire process sequence consists of process 18181: Sara@dell:~/OSSS$ ./linear 1 Creating process sequence of length 1. 18181 begins the sequence. An example for a sequence of length three: Sara@dell:~/OSSS$ ./linear 3 Creating process sequence of length 3. 18233 begins the sequence. 18234 is child of 18233 18235 is child of 18234 ........ this is coad .... BUt i could not compleate it .....:( #include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char *argv[]) { int N; pid_t pid; int cont; if (argc != 2) { printf("Wrong number of command-line parameters!\n"); return 1; } N = atoi(argv[1]); printf("Creating process sequence of length %d.\n",N); printf("%d begins the sequence.\n",getpid()); /* What I have to do next ?????? */ }

0 Answers  


What is infinite loop?

0 Answers  


What is a lookup table in c?

0 Answers  


Categories