Here is a comment stripper program from which takes input from Cin and removes the comments.

#include <iostream>
#include <fstream>

char Getchar();
char var;
int main()
{
	char ch;
	do
	{
		Getchar();
		switch(var)
		{
			case '/':
			switch(Getchar())
			{
				case '*':
					Getchar();
					while(true)
					{
						if(var=='*'&&Getchar()=='/')
						{
							break;
						}
						else
						{
							Getchar();
						}
					}
					break;

				case '/':
					while(Getchar()!='\n');
					std::cout<<'\n';
					break;

				default:
					break;
	
			}
			break;
			case '"':
			do
			{
				std::cout<<var;	
			}
			while (Getchar()!='"');
			std::cout<<var;
			break;

			default:
			std::cout<<var;
		}
			
	}
	while(var!='x');
	
}

char Getchar()
{
	 std::cin.get(var);
	 return var;
}

So any improvements ?
Secondly I am looking for another way by which i can close input any suggestions on that.
I mean this program exits when "x" is inputted. Is there any other way of Exiting?

Recommended Answers

All 22 Replies

Oh, so all it does is strip comments from keyboard input and not from a *.c or *.cpp data file??? Why would you want to write such a program?


>>I mean this program exits when "x" is inputted. Is there any other way of Exiting?

Just press Ctrl+C.

Oh, so all it does is strip comments from keyboard input and not from a *.c or *.cpp data file??? Why would you want to write such a program?


>>I mean this program exits when "x" is inputted. Is there any other way of Exiting?

Just press Ctrl+C.

Wouldnt I be Able To Replace cin and cout with filestreams and remove comments from .c or .cpp files

This is an exercise in my c++ book so I just wrote it down. As I am teaching myself C++ i post down the code and ask you guys for improvements :)

What does CTRL+C do?

Improvements? Try to process this (correct) line in C++ and see what happens:

ifstream file("//please/dont/strip.me");

;)

Improvements? Try to process this (correct) line in C++ and see what happens:

ifstream file("//please/dont/strip.me");

;)

It returns

ifstream("//please/dont/strip.me");

What does CTRL+C do?

CTRL+C just breaks/stops the execution of a console program :)

By the way: I had also expected your program was stripping comments from C/C++ source files, but you can easily adapt it to remove comments from files :)

commented: Thanks :) +3

So any improvements ?

Yes. Stop using TABs to indent. Set your editor to use 4 SPACEs when the TAB key is pressed.

As for exiting, you can exit when ENTER is pressed all by itself.

And about Ctrl+C, look it up.

commented: I didn't know that about the TABs, I'll surely try it :) +4
commented: Done !! Thanks :) +3

It returns...

Oh, that damned tab indentation! ;)
OK, try this:

//\
strip me!

Wouldnt I be Able To Replace cin and cout with filestreams and remove comments from .c or .cpp files

Actuall you don't have to change a thing to make it get input from a data file instead of the keyboard -- just direct stdin from data file, like this: c:>myprogram.exe <test.cpp <Return> When the above is typed from a command prompt window your program will get input from the file test.cpp instead of the keyboard.

Actuall you don't have to change a thing to make it get input from a data file instead of the keyboard -- just direct stdin from data file, like this: c:>myprogram.exe <test.cpp <Return> When the above is typed from a command prompt window your program will get input from the file test.cpp instead of the keyboard.

But it won't strip the comments out of the file ...

If you want the comments stripped from the file then just redirect stdout to another file c:>myprogram.exe <test.cpp >newfile.cpp <Return>

Thats really Nice to Know :)

Will that work on all platforms

Is this just like using Pipes to pass information

for example
WIndows.

C:> dir >>output.txt

Would put all the output of the Dir command In Output.txt

Will that work on all platforms

I know that it works on Windows, Linux and Unix :P

for example
WIndows.

C:> dir >>output.txt

Would put all the output of the Dir command In Output.txt

Yes, but remember that >> is different from > : >> appends to the file (if it exists, otherwise it's just creating the file and writing the output of the dir command to it)

For further info on this: http://commandwindows.com/command1.htm

Other failed tests:

f("\""); // Strip me!
ch = '\"'; // Strip me!

Probably, this is too much!(?) :

#if 0
/* Code wrecks...
#endif
cout << "Hello, Sky Diploma!\n";
Member Avatar for iamthwee

To echo ArkM there are going to be a lot of exceptions.

The best/easiest way would be to use regular expressions via the boost library.

To echo ArkM there are going to be a lot of exceptions.
The best/easiest way would be to use regular expressions via the boost library.

In no circumstances.
There are few syntax contexts where comments occured, so reduced scanner+parser tandem which is capable to strip comments is a very simple and fast code w/o (relatively) cumbersome and slow regex.
However it's not so easy to cope with macros...

I agree that I dint consider the /" .

And I

ch = '\"'; // Strip me!

And thought that that was invalid.
So i guess i will work on that as well :)

Some additions:
>...to be a lot of exceptions.
Nope.
Essentially, Sky Diploma's code does not recognized correctly three very simple lexical-level constructs:
1. string literals
2. character literals
3. line continuation
Besides that it has a very strange termination condition (letter x - why?! ) and can't react to eof condition...

Member Avatar for iamthwee

There are cases :

"Since comments can be embedded in strings, or look like function prototypes, care must be taken to ignore these cases. "
http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.27.html

etc which would make this program very messy.

Regex is the ideal solution. But if you wish to prove me wrong please do.

#include <iostream>
#include <fstream>

char Getchar();
char var;
int main()
{
	do{
		Getchar();
		switch(var)
		{
			case '/':
			switch(Getchar())
			{
				case '*':
					Getchar();
					while(true)
					{
						if(var=='*'&&Getchar()=='/')
						{
							break;
						}
						else
						{
							Getchar();
						}
					}
					break;

				case '/':
					while(Getchar()!='\n');
					std::cout<<'\n';
					break;

				default:
					break;
	
			}
			break;
			case '\'':
			std::cout<<var;
			Getchar();
			do
			{
			if(var=='\\'&&Getchar()=='\'')
				{
				std::cout<<'\\';

				}
			std::cout<<var;
			}
			while (Getchar()!='\'');
			std::cout<<var;
			break;
			case '"':
			do
			{
				if(var=='\\'&&Getchar()=='"')
				{
				std::cout<<'\\';

				}
				std::cout<<var;
				Getchar();	
			}	while (var!='"');
			std::cout<<var;
			break;
			default:
			std::cout<<var;
		}
			
	}
	while(std::cin);
	
}

char Getchar()
{
	 std::cin.get(var);
	 return var;
}

This is like the improvised function.
However i still am not able to crack "\//" AND "/*................." Till end of file though.
Please Help me out with that .

Though I dint test it through a lot of Code. I Think that this would be enough. But would it?

You could also use the following code to strip all the C++ style comments:

char expr[80]; // user inputted expression (with/without comments)
/* Strip down the '//'-comments */
char *p = strstr(expr, "//");
if(p) *p='\0';

expr now contains the expression without all the C++ comments :)

The C-style comments are a bit harder to do, but it's do-able :P

You could also use the following code to strip all the C++ style comments:

char expr[80]; // user inputted expression (with/without comments)
/* Strip down the '//'-comments */
char *p = strstr(expr, "//");
if(p) *p='\0';

expr now contains the expression without all the C++ comments :)

The C-style comments are a bit harder to do, but it's do-able :P

I dont think that this will work out.

string s="foldername//file";

The above function would remove the file"; part.

I dont think that this will work out.

string s="foldername//file";

The above function would remove the file"; part.

True, but keep in mind that this way doesn't work directly with C++ strings :) (however with a few modifications you can make it also work for C++ strings :))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.