Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

remove all comments function in c

/**
 Tuesday, 10/07/2013

 Exercise 1.23
 Write a program to remove all comments from a C 
 program. Don't forget to handle quoted strings 
 and character constants properly. C comments   
 don't nest.
**/

#include <stdio.h>
#define MAX_LENGTH 65536
#define NOT_IN_COMMENT 0
#define SINGLE_COMMENT 1
#define MULTI_COMMENT  2

main()
{
    char code[MAX_LENGTH];        /* Buffer that stores the inputted code */
    int size = 0;                 /* Length of the inputted code */
    int loop;                     /* Integer used for the for loop */
    char c;                       /* Character to input into */
    int status = NOT_IN_COMMENT;  /* Are we in a comment? What type? */
    int in_string = 0;            /* Are we inside of a string constant? */
    char last_character;          /* Value of the last character */


    /* Input all code into the buffer until escape sequence pressed */
    while ((c = getchar()) != EOF)
        code[size++] = c; 
    code[size] = ''; 


    /* Remove all comments from the code and display results to user */
    for (loop = 0; loop < size; loop++) {
        char current = code[loop]; 

        if (in_string) {
            if (current == '"') in_string = 0; 
            putchar(current);
        }

        else {
            if (status == NOT_IN_COMMENT) {
                if (current == '"') {
                    putchar(current);
                    in_string = 1; 
                    continue; 
                }

                if (current == '/' && last_character == '/') status = SINGLE_COMMENT;
                else if (current == '*' && last_character == '/') status = MULTI_COMMENT; 
                else if (current != '/' || (current == '/' && loop < size-1 && !(code[loop+1] == '/' || code[loop+1] == '*'))) putchar(current); 
            }

            else if (status == SINGLE_COMMENT) {
                if (current == '
') {
                    status = NOT_IN_COMMENT; 
                    putchar('
');
                }
            }

            else if (status == MULTI_COMMENT) {
                if (current == '/' && last_character == '*') status = NOT_IN_COMMENT; 
            }
        }

        last_character = current; 
    }
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: common mistakes in testing 
Typescript :: how to check if data attribute exists in jquery 
Typescript :: NFS is reporting that your exports file is invalid. Vagrant does this check before making any changes to the file. Please correct the issues below and execute "vagrant reload": 
Typescript :: typescript append row in html table 
Typescript :: access single document with its id flutter 
Typescript :: wordpress number of posts by user 
Typescript :: how to create empty object typescript 
Typescript :: Already included file name react tsconfig 
Typescript :: subplots in for loop python 
Typescript :: python convert long floats to usd 
Typescript :: typescript object destructuring 
Typescript :: define typescript variable types 
Typescript :: stripe create customer 
Typescript :: pass function as argument typescript 
Typescript :: typescript object type 
Typescript :: concat type typescript 
Typescript :: typescript function return type observable 
Typescript :: auto complete of process.env in typescript 
Typescript :: stripe create subscription 
Typescript :: remove showing results woocommerce shortcode 
Typescript :: ng idle issue ERROR in node_modules/@ng-idle/core/lib/eventtargetinterruptsource.d.ts(29,9): error TS1086: An accessor cannot be declared in an ambient context. 
Typescript :: prototype design pattern typescript 
Typescript :: Scripts cannot be executed on this system. 
Typescript :: python remove all double elements from list 
Typescript :: typescript class 
Typescript :: google sheets query multiple or 
Typescript :: tsconfig-paths/register mocha 
Typescript :: class in typescript 
Typescript :: powerpoint presentation are widely used as 
Typescript :: IM DEAD 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =