•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 456,552 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,453 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 514 | Replies: 1
![]() |
•
•
Join Date: Sep 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
this code removes comments
please help in correcting the code
//Program to print a file by removing Comments
please help in correcting the code
//Program to print a file by removing Comments
c Syntax (Toggle Plain Text)
[inlinecode]#include<stdio.h> void main() { FILE *fp; char ch; //clrscr(); fp=fopen("temp.txt","r"); while(1) { ch=fgetc(fp); if(ch==EOF) break; else { if(ch=='/'){ ch=fgetc(fp); if(ch=='/') { while(1) { ch=fgetc(fp); if(ch=='\n') goto label; } } if(ch=='*') { while(1) { ch=fgetc(fp); if(ch=='*') { ch=fgetc(fp); if(ch=='/') { while(1) { ch=fgetc(fp); goto label; } } else printf("*"); } } } else printf("/"); } } label:printf("%c",ch); } fclose(fp); }[/inlinecode]
Last edited by rohit83.ken : Oct 20th, 2007 at 10:09 am.
Add a FSM to your program.
Eg.
Then you do say
Once you've got the idea of wandering round a FSM diagram (you examine the current state and the next character, then decide what to do about it).
A key test for you will be to do the right thing for this code
Can you think what extra states you would need to handle this case?
Eg.
enum state_t {
normal,
comment_start,
in_comment,
comment_end
};Then you do say
int ch;
state_t myState = normal;
while ( (ch=fgetc(fp)) != EOF ) {
if ( state == normal ) {
if ( ch == '/' ) {
// could be the start of a comment, advance state
state = comment_start;
} else {
// any other char, just print it
}
} else
if ( state == comment_start ) {
if ( ch == '*' ) {
// really is the start of a comment
state = in_comment;
} else {
// the / was not part of "/*", so print it and revert back to normal
state = normal;
}
} else
if ( state == comment_end ) {
}
}A key test for you will be to do the right thing for this code
int main ( ) {
printf( "/* this is a comment */\n" );
return 0;
}![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Hardware Interrupts & 100% CPU usage (Windows NT / 2000 / XP / 2003)
- Pascal's Triangle in C (C)
- How to learn Java (Java)
- Help with replace method. (Java)
- The Logic of This Code is Skewed Perhaps: (C++)
- eMac won't let me install OS or anything else! (OS X)
- Custom pascal triangle ? (C)
- substitue of comparison operators (C++)
- stack palindrome problem? (C++)
Other Threads in the C Forum
- Previous Thread: Inverse of a matrix
- Next Thread: problems with a code made in linux working in windows



Linear Mode