Untitled

 avatar
plain_text
14 hours ago
1.2 KB
1
Indexable
#include <stdio.h>

int main() {
    FILE *fp = fopen("hello.c", "r");
    char c, n;

    if (!fp) return 1;

    while ((c = fgetc(fp)) != EOF) {
        if (c == '/') {
            n = fgetc(fp);

            if (n == '/') {                  // Single-line comment
                while ((c = fgetc(fp)) != EOF && c != '\n');
                if (c == '\n') putchar('\n');
            }
            else if (n == '*') {             // Multi-line comment
                while ((c = fgetc(fp)) != EOF)
                    if (c == '*' && (n = fgetc(fp)) == '/')
                        break;
                    else
                        ungetc(n, fp);
            }
            else {
                putchar(c);
                ungetc(n, fp);
            }
        }
        else
            putchar(c);
    }

    fclose(fp);
    return 0;
}
Editor is loading...
Leave a Comment