User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Sep 2007
Posts: 6
Reputation: rohit83.ken is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
rohit83.ken rohit83.ken is offline Offline
Newbie Poster

Help problem in the logic

  #1  
Oct 20th, 2007
this code removes comments

please help in correcting the code


//Program to print a file by removing Comments
  1. [inlinecode]#include<stdio.h>
  2. void main()
  3. {
  4. FILE *fp;
  5. char ch;
  6. //clrscr();
  7. fp=fopen("temp.txt","r");
  8. while(1)
  9. {
  10. ch=fgetc(fp);
  11.  
  12. if(ch==EOF)
  13. break;
  14. else
  15. {
  16. if(ch=='/'){
  17. ch=fgetc(fp);
  18. if(ch=='/')
  19. {
  20. while(1)
  21. {
  22. ch=fgetc(fp);
  23. if(ch=='\n')
  24. goto label;
  25. }
  26. }
  27. if(ch=='*')
  28. {
  29. while(1)
  30. {
  31. ch=fgetc(fp);
  32. if(ch=='*')
  33. {
  34. ch=fgetc(fp);
  35. if(ch=='/')
  36. {
  37. while(1)
  38. {
  39. ch=fgetc(fp);
  40. goto label;
  41. }
  42. }
  43. else printf("*");
  44. }
  45. }
  46. }
  47. else printf("/");
  48. }
  49. }
  50. label:printf("%c",ch);
  51. }
  52. fclose(fp);
  53. }[/inlinecode]
Last edited by rohit83.ken : Oct 20th, 2007 at 10:09 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2005
Posts: 3,834
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 436
Colleague
Salem's Avatar
Salem Salem is offline Offline
banned

Re: problem in the logic

  #2  
Oct 20th, 2007
Add a FSM to your program.

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 ) {
  }
}
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
int main ( ) {
  printf( "/* this is a comment */\n" );
  return 0;
}
Can you think what extra states you would need to handle this case?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 5:15 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC