GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C
Go To First  |  Previous Question  |  Next Question 
 C  |  Question 436 of 453    Print  
Strip Comments
Write a program to read a file, strip the comments and write the output to another file.


  
Total Answers and Comments: 2 Last Update: February 03, 2009     Asked by: mehaboob.p 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: dima2000star
 

{

FILE *f, *of;

f = fopen("c:/myprog1.cpp", "r");

of = fopen("c:/comments.cpp", "a");

char* ch = new char[2];

*ch = fgetc(f);

int l=0;while (!feof(f))

{

if ((*ch == '/') && (fgetc(f)=='/'))

{

l++;

do

{

*ch = fgetc(f);

fputc(*ch, of);

printf("%d , %c", *ch, *ch);

getchar();

}

while (*ch != 10);

}

*ch = fgetc(f);

}

printf(
"number of comments: %d", l);

getchar();

}



Above answer was rated as good by the following members:
valliis
September 18, 2008 08:55:00   #1  
dima2000star Member Since: September 2008   Contribution: 1    

RE: Strip Comments

{

FILE *f *of;

f fopen("c:/myprog1.cpp" "r");

of fopen("c:/comments.cpp" "a");

char* ch new char[2];

*ch fgetc(f);

int l 0;while (!feof(f))

{

if ((*ch '/') && (fgetc(f) '/'))

{

l++;

do

{

*ch fgetc(f);

fputc(*ch of);

printf(" d c" *ch *ch);

getchar();

}

while (*ch ! 10);

}

*ch fgetc(f);

}

printf(
"number of comments: d" l);

getchar();

}


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
February 02, 2009 17:35:34   #2  
nemuns Member Since: January 2009   Contribution: 3    

RE: Strip Comments

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

enum ParseState
{
INITIAL
ONE_SLASH_READ
TWO_SLASHES_READ
ONE_SLASH_STAR_READ
ONE_SLASH_STAR_AND_STAR_READ
} CommentParseState;

StripComment()
{
FILE * fp;
FILE *fn;
char ch[1000] buffer[100];;
int buf j i 0;
int temp 0;

CommentParseState INITIAL;

fp fopen("C:\Documents and Settings\Mini\My Documents\Mini\Programs\Prog Usable\hashing.c" "r");
fn fopen("C:\Documents and Settings\Mini\My Documents\Mini\Programs\Prog Usable\test.c" "w+a");

while((buf getc(fp)) ! EOF)
{
switch (buf)
{
case '/':
if (CommentParseState INITIAL)
{
CommentParseState ONE_SLASH_READ;
}
else if (CommentParseState ONE_SLASH_READ)
{
//CommentParseState TWO_SLASHES_READ;

// Read upto the End of File or the End of Line
while(1)
{
buf getc(fp);
if ((buf EOF) || (buf 10))
{
break;
}
}
CommentParseState INITIAL;
}
else if(CommentParseState ONE_SLASH_STAR_AND_STAR_READ)
{
CommentParseState INITIAL;
}
break;

case '*':
if (CommentParseState ONE_SLASH_READ)
{
while(1)
{
buf getc(fp);
buffer[i] buf;
i++;
if((buf 42)|| (buf EOF)) //Check for *
{
CommentParseState ONE_SLASH_STAR_AND_STAR_READ;
break;
}
else if(buf '/')
{
CommentParseState INITIAL;

for(j 0; j<i; j++)
{
temp buffer[j];
putchar(temp);
}
break;
}
}
}
else
{
putchar(buf);
}

if((buf EOF))
{
putchar('/');
putchar('*');
for(j 0; j<i; j++)
{
temp buffer[j];
putchar(temp);
}
}

break;

default:
if (CommentParseState INITIAL)
{
putchar(buf);
}
else if (CommentParseState ONE_SLASH_READ)
{
CommentParseState INITIAL;
putchar('/');
putchar(buf);
}
else if (CommentParseState ONE_SLASH_STAR_AND_STAR_READ)
{
CommentParseState INITIAL;
for(j 0; j<i; j++)
{
temp buffer[j];
putchar(temp);

}

}
break;
}
}
}


 
Is this answer useful? Yes | No

 Related Questions

Latest Answer : working code ************************#includemain(){ FILE *f; char ch;int c=0; f=fopen("reddy", "r"); ch=fgetc(f); while(!feof(f)) { switch(ch) { case 47 : ...
Read Answers (4) | Asked by : lizz

Write a program to read a file, strip the comments and write the output to another file. 
Latest Answer : #include#include#includeenum ParseState {  INITIAL,  ONE_SLASH_READ,  TWO_SLASHES_READ,  ONE_SLASH_STAR_READ,  ONE_SLASH_STAR_AND_STAR_READ } CommentParseState;StripComment(){ FILE ...


 Sponsored Links

 
Related Articles

Concepts of Object-Oriented Programming

Object Oriented JavaScript In this chapter you ll learn about OOP Object Oriented Programming and how it relates to JavaScript As an ASP NET developer you probably have some experience working with objects and you may even be familiar with concepts such as inheritance However unless you re already a
 

SQL Programming

SQL Programming Overview Anybody who has done something for a long time has probably wanted to change how things work at some point or another. A worker at a mill might have found a more efficient way of cutting logs, or a mathematics teacher might have had a hand in changing a school&rsquo;s al
 

The Interview Snafu

How to turn someone else&rsquo;s mistake to your advantage Your dream job is about to become reality. A recruiter gave you the heads up about the perfect position at Humungous Conglomerate, Inc. You went through five interviews as well as a battery of psychological tests mandated by their HR de
 

Winning a Job Interview with a Winning Resume

Does your resume unlock your potential, take your skills to the highest level and win you the interview and the job you want now? The job market today is highly competitive and even if you think you have what it takes to get an interview you won&rsquo;t get over the line without a polished, prof
 

WinRunner Programming Concepts

If you want to create WinRunner scripts that are highly efficient, there are important programming concepts that you will want to become familiar with. Understanding these concepts will provide you with a large number of key benefits. In addition to understanding these concepts, you must also learn
 

Programming Languages Certification

IT Certification programs have several options that will offer you the best knowledge.&nbsp; By learning everything that you need to know about information technology you will be able to open new doors to your career and personal business desires.&nbsp; IT Certification offers several vari
 

Neuro-linguistic Programming Methods

Neuro linguistic Programming Methods There are several methods used for performing Neuro linguistic Programming on an individual for obtaining insights into the psyche of the person in order to correct to modify certain patterns of behavior These techniques are also used for Neuro linguistic trainin
 

Importance of Proper English during Job Interview

Importance of Proper English during Job Interview Your job interview is crucially important and it will determine whether or not you will get the job Depending on the type of job you re going for it is very important for you to use proper English In most cases jobs which offer higher salaries will h
 

The Difficult Past of Neuro-linguistic programming

The Difficult Past of Neuro linguistic programming Neuro linguistic programming has had a rocky past with a number of lawsuits rivalry unsystematic development and intermittent progress During the 1980s the two founders separated after the lawsuit filed by Bandler Bandler went on to file several law
 

History of Neuro Linguistic Programming (NLP)

History of Neuro linguistic programming Neuro Linguistic Programming NLP is a system co founded by John Grinder and Richard Bandler for bringing about personal development in any individual by interpreting and translating the human perception experience beliefs and language It was developed in early
 

About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape