C Program for remove comments from source file of all Programming Languages

C Program to remove comments from source code

A basic C Program to just remove all types comments from any source code file. Its written in c language and take file name as parameter when run it. Its read every character of code and remove comments to generate a clean code. This code can be compile and run using any compiler.

#include<iostream.h>
#include<conio.h>
#include<fstream.h>

int main(int argc, char * argv[]) {
  char ch, ch1;
  ifstream f1;
  ofstream f2;
  f1.open(argv[1]);
  f2.open(argv[2]);
  f1.get(ch);
  ch1 = ch;
  do {
    f1.get(ch);
    if (ch1 == '/' && ch == '*') {
      do {
        f1.get(ch);
      } while (ch != '*');
    } else if (ch == '/') {
      do {
        f1.get(ch);
      } while (ch != '\n');
      f2 << '\n';
    } else
      f2 << ch;
  } while (f1);
  f1.close();
  f2.close();
  return 0;
}

It is a simple program to read source code file and remove all type comments and print output on command screen.

Output of sample source code after removing comments.
Output of sample source code after removing comments.

How to run:-

  1. First compile this program using your c/cpp compiler and create binary (.exe) file.
  2. Then open cmd and write your binary file name then space and now enter your source code file path.
  3. Now hit Enter.

Now the output will be on your screen without any comments.

You will also like full functional Linked List Program A Linked list Program in CPP with complete Functionality


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *