4

I am trying to create a function which removes C-style comments from a string.

I tried using the classic while-loop when using pointers to go through a char array, now by using two pointers(p2 is a step ahead of p1, and they both start at the beginning). I tried checking*p1 == '/' and when *p2 == '*', and when it was true I'd create two new pointers which would find the end of the comment (*p3 == '*' && *p4 == '/'), and then I'd create two new pointers p5 and p6 , the first of which would point to p1(the first character in the comment, which should be '/', and the second would point to p4(the last character which would be removed), I'd use a while loop to try and

do while(*p5++ = *p6++);

which sadly does not work.

#include <stdio.h>

void remove_comments(char* s) {

    char *p1=s;
    char *p2 = s;   
        p2++; // move p2 ahead of p1;


    while(*p1 !='\0' && *p2 !='\0') { 

        if(*p1=='/' && *p2=='*') { // beginning of comment
            char *p3 = p1; // save their positions and
            char *p4 = p2; //create two new pointers

            while(*p3 !='\0' && *p4 !='\0') {
            if(*p3 == '*' && *p4 == '/') { //if end of comment

                    char *p5 = p1;
                    char *p6 = p4;

                    while(*p5++ = *p6++);

                } else p1++;

                p3++;
                p4++;

            }




        }
        p1++;
        p2++;


    }




    }




int main() {


    char arr[] = "This is an /*this is a comment*/ example!";

    remove_comments(arr);

    printf("%s", arr);



    return 0;
}

The code above should produce the output This is an example!, but the string remains unchanged. How do I fix this?

8
  • 2
    It's an ideal time for you to learn to use a debugger. Aug 26, 2019 at 18:24
  • 2
    This is a common exercise from K&R and there are numerous other examples including ones that stream directly from getchar(). This question is essentially caused by a typo and hence not very interesting. Aug 26, 2019 at 18:27
  • @AnttiHaapala It's also caused by an unneeded increment of s1.
    – S.S. Anne
    Aug 26, 2019 at 18:31
  • Also please check your compiler diagnostics output - a conforming compiler must issue warning for the first mistake. And I suppose you're not using Turbo C 2.0 because that wouldn't work well with Valgrind! Aug 26, 2019 at 18:32
  • 1
    A good strategy for this kind of problem is to draw a state machine.
    – Caleb
    Aug 26, 2019 at 18:49

1 Answer 1

1

Turn up your compiler warnings (gcc -Wall).

This (from a previous revision):

test.c: In function ‘remove_comments’:
test.c:11:29: warning: comparison between pointer and integer
         if(*p1 == '/' && p2 == '*')
                             ^~

makes one part of the issue really obvious (it can't detect the sequence).

Change this:

        if(*p1=='/' && p2=='*') { // beginning of comment

to this:

        if(*p1=='/' && *p2=='*') { // beginning of comment

You're incrementing s1 when you don't need to, and this causes an error.

Remove this:

                else
                    p1++;

Along with that, the code here:

                if(*p3 == '*' && *p4 == '/')
                { // if end of comment

                    char *p5 = p1;
                    char *p6 = p4;

is copying the slash from p4.

Change that chunk to this:

                if(*p3 == '*' && *p4 == '/')
                { // if end of comment

                    char *p5 = p1;
                    char *p6 = p4+1;

and, combined with the other changes, your code works.

I suggest changing while(*p5++ = *p6++); to strcpy(p5, p6);; strcpy may well be faster.

It doesn't produce your exact desired output, but it removes all characters between /* and */, inclusive.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.