Advertisement
Guest User

1-23

a guest
Dec 18th, 2012
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.08 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int main () {
  4.  
  5.     int c,next,state,read;
  6.  
  7.     enum {program = 1, comment, line_comment, squote,dquote,s_escape,d_escape}; /*s_escape is escape from a single quote and d_escape from double*/
  8.  
  9.     state = program; //this is the default state
  10.  
  11.     while ((c = getchar()) != EOF) {
  12.  
  13.         if (c == '\'') { /*if you come across a single quote*/
  14.  
  15.             if (state == program)
  16.    
  17.                 state = squote;
  18.  
  19.             else if (state == squote)
  20.  
  21.                 state = program;
  22.  
  23.             else if (state == dquote)
  24.  
  25.                 state = dquote;
  26.  
  27.             else if (state == s_escape)
  28.  
  29.                 state = squote;
  30.  
  31.             else if (state == d_escape)
  32.  
  33.                 state = dquote;
  34.  
  35.             else
  36.  
  37.                 ;
  38.    
  39.             if (state != comment && state != line_comment)
  40.  
  41.                 putchar(c);
  42.            
  43.                 }
  44.  
  45.  
  46.         else if (c == '"') {
  47.  
  48.             if (state == program)
  49.  
  50.                 state = dquote;
  51.  
  52.             else if (state == squote)
  53.  
  54.                 state = squote;
  55.  
  56.             else if (state == dquote)
  57.  
  58.                 state = program;
  59.  
  60.             else if (state == s_escape)
  61.  
  62.                 state = squote;
  63.  
  64.             else if (state == d_escape)
  65.  
  66.                 state = dquote;
  67.  
  68.             else
  69.            
  70.                 ;
  71.  
  72.             if (state != comment && state != line_comment)
  73.  
  74.                 putchar(c);
  75.  
  76.                     }
  77.  
  78.         else if (c == '\\') {
  79.  
  80.             if (state == squote)
  81.  
  82.                 state = s_escape;
  83.  
  84.             else if (state == dquote)
  85.  
  86.                 state = d_escape;
  87.  
  88.             else if (state == s_escape)
  89.  
  90.                 state = squote;
  91.  
  92.             else if (state == d_escape)
  93.  
  94.                 state = dquote;
  95.  
  96.             else if (state == program)
  97.  
  98.                 state = program;
  99.  
  100.             else
  101.        
  102.                 ;
  103.  
  104.             if (state != comment && state != line_comment)
  105.  
  106.                 putchar(c);
  107.  
  108.  
  109.                      }
  110.  
  111.         else if (c == '/') {
  112.  
  113.             if (state == program) {
  114.  
  115.                 if ((next = getchar()) == '*')
  116.  
  117.                     state = comment;
  118.  
  119.                 else if (next == '/') {
  120.  
  121.                     state = line_comment; //this is an example of a line_comment
  122.                    
  123.                     while ((read = getchar()) != '\n')
  124.  
  125.                         ;
  126.  
  127.                     if (read == '\n')
  128.  
  129.                         putchar('\n');
  130.  
  131.                     state = program;
  132.  
  133.                               }
  134.  
  135.                 else {
  136.  
  137.                     state = program;
  138.  
  139.                     putchar('/');
  140.  
  141.                     putchar(next);
  142.  
  143.                      }
  144.                           }
  145.  
  146.             else if (state == squote || state == dquote)
  147.  
  148.                 putchar('/');
  149.                
  150.             else if (state == s_escape) {
  151.  
  152.                 state = squote;
  153.  
  154.                 putchar('/');
  155.        
  156.                              }
  157.  
  158.             else if (state == d_escape) {
  159.  
  160.                 state = dquote;
  161.  
  162.                 putchar('/');
  163.        
  164.                              }
  165.                                      
  166.  
  167.             else
  168.  
  169.                 ;
  170.                     }
  171.  
  172.         else if (c == '*') {
  173.  
  174.             if (state == comment) {
  175.  
  176.                 if ((next = getchar()) == '/')
  177.  
  178.                     state = program;
  179.  
  180.                 continue;      
  181.  
  182.                                }   
  183.  
  184.             else if (state == squote || state == dquote || state == program)
  185.  
  186.                 putchar('*');
  187.  
  188.             else if (state == s_escape) {
  189.  
  190.                 state = squote;
  191.  
  192.                 putchar('*');          
  193.  
  194.                              }
  195.  
  196.             else if (state == d_escape) {
  197.  
  198.                 state = dquote;
  199.  
  200.                 putchar('*');
  201.  
  202.                              }
  203.  
  204.             else
  205.  
  206.                 ;
  207.  
  208.  
  209.                    }
  210.  
  211.         else {
  212.  
  213.             if (state != comment && state != line_comment) {
  214.  
  215.                 putchar(c);
  216.  
  217.                 if (state == s_escape)
  218.  
  219.                     state = squote;
  220.  
  221.                 else if (state == d_escape)
  222.  
  223.                     state = dquote;
  224.            
  225.  
  226.                                         }
  227.            
  228.               }
  229.                             }
  230.     return 0;
  231.          }
  232. //another line comment
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement