Advertisement
STANAANDREY

remove comments from c/cpp prog

Nov 16th, 2022 (edited)
1,182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #define CAN_PRINT 0
  3. #define MET_SLASH 1
  4. #define MET_LINE_COMM 2
  5. #define MET_BLOCK_COMM 3
  6. #define MET_END_BLOCK_STAR 4
  7.  
  8.  
  9. int main() {
  10.   short state = CAN_PRINT;
  11.   for (char ch; (ch = getchar()) != EOF;) {
  12.     if (ch == '/' && state == CAN_PRINT) {
  13.       state = MET_SLASH;
  14.     } else if (ch == '/' && state == MET_SLASH)  {
  15.       state = MET_LINE_COMM;
  16.     } else if (ch == '*' && state == MET_SLASH) {
  17.       state = MET_BLOCK_COMM;
  18.     } else if (ch == '\n' && state == MET_LINE_COMM) {
  19.       state = CAN_PRINT;
  20.     } else if (ch == '*' && state == MET_BLOCK_COMM) {
  21.       state = MET_END_BLOCK_STAR;
  22.     } else if (ch == '/' && state == MET_END_BLOCK_STAR) {
  23.       state = CAN_PRINT;
  24.       continue;
  25.     } else if (ch != '/' && state == MET_SLASH) {
  26.       putchar('/');
  27.       state = CAN_PRINT;
  28.     } else if (ch != '/' && state == MET_END_BLOCK_STAR) {
  29.       state = MET_BLOCK_COMM;
  30.     }
  31.    
  32.     putchar(state == CAN_PRINT ? ch : 0);
  33.   }
  34.   return 0;
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement