Question

This is about the comment deletion program in Kernighan and Ritchie (number 1-23 p 34 ANSI eidtion). The following is my solution.

http://pastebin.com/Fu9C94fV

The program works fine with most C programs, deleting comments. However, in some programs with comments that end with multiple asterisks (**/), it poses problems (does not read through). One such example is this one :

http://pastebin.com/J8EQrwve

However, it works with this program, despite the fact that it has a similar comment at the beginning:

http://pastebin.com/thWKvDS7

So what do you think is causing the comment deletion program to behave this way?

Was it helpful?

Solution

When you hit a * in comment state, you read the next character to see if it's a /. This consumes that character, so it won't be checked on the next iteration.

So:

/*  **/
    12

sees the * at 1, reads the * at 2, stays in comment mode, and continues with the /. Whereas:

/* ***/
   123

sees the * at 2, reads the * at 2, continues, then reads the * at 3 and finds a / following and drops out of comment mode.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top