Advertisement
STANAANDREY

remove comments (nested loops)

Nov 20th, 2022
781
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void skipLineComm() {
  5.   for (char ch; (ch = getchar()) != EOF;) {
  6.     if (ch == '\n') {
  7.       putchar('\n');
  8.       return;
  9.     }
  10.    
  11.   }
  12. }
  13.  
  14. void skipBlockComm() {
  15.   for (char ch, last = 0; (ch = getchar()) != EOF; last = ch) {
  16.     if (ch == '/' && last == '*') {
  17.       return;
  18.     }
  19.   }
  20. }
  21.  
  22. int main(void) {
  23.   for (char ch, last = 0; (ch = getchar()) != EOF; last = ch) {
  24.     if (ch == '/' && last != '/') {
  25.       continue;
  26.     }
  27.    
  28.     if (ch == '/' && last == '/') {
  29.       skipLineComm();
  30.       ch = last = 0;
  31.     }
  32.     if (ch == '*' && last == '/') {
  33.       skipBlockComm();
  34.       ch = last = 0;
  35.     }
  36.    
  37.     if (last == '/') {
  38.       putchar(last);
  39.     }//*/
  40.  
  41.     putchar(ch);
  42.   }
  43.   return 0;
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement