0

I'm trying to write a function that extracts the comment out of a string. For example, given:

"this is a test //bread is great"

it returns:

"bread is great"

I've tried to count the characters until the first '//' appears and then trim the unwanted part of the string.

while(s[i] != '/' && s[i+1] != '/') {
    newbase++;
    i++;
}

It worked for the first example but I'm having issues if I'm given a string like this:

"int test = 2/3"

It should return "" (an empty string) but it doesn't. I don't understand it.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • `while (s[i] != '\0' && s[i+1] != '\0' && ...` and remember to check `s[i]` after the loop terminates – pmg Dec 09 '21 at 14:05
  • 2
    Shouldn't that `&&` be an `||`? – Ian Abbott Dec 09 '21 at 14:07
  • 3
    you can use `strchr` to see if the string contains `//` in the first place. – eglease Dec 09 '21 at 14:07
  • 1
    If you encounter the end of string before the pattern is found, you should stop your search. –  Dec 09 '21 at 14:07
  • you gonna have a problem with `printf("I'm a string with an // embedded comment");` or `x = 0; /* was -1 // reset x */` – pmg Dec 09 '21 at 14:08
  • See https://stackoverflow.com/questions/11836524/in-c-find-position-of-substring-in-a-string – eglease Dec 09 '21 at 14:09
  • 1
    You can use strstr – Eric Marchand Dec 09 '21 at 14:12
  • 1
    Suppose the input string is `"Text // Comment\nMore Information"` — how much of that is deemed 'comment'? To the C compiler, only 'Comment' would count as a comment; it seems likely that your code would include 'More Information' too. It depends on what the problem specification requires. Incidentally, to a C compiler, the whole of `"/\\\n\\\n/\\\nComment\n"` is a comment. – Jonathan Leffler Dec 09 '21 at 14:13

2 Answers2

2

This is very basic string handling. Simply use strstr and if successful, use the result. Optionally copy it to a second string.

#include <stdio.h>
#include <string.h>

int main (void)
{
  const char* str = "this is a test //bread is great";
  const char* result = strstr(str,"//");

  if(result != NULL)
  {
    result += 2; // skip the // characters
    puts(result); // print the string
    
    // optionally make a hardcopy
    char some_other_str[128];
    strcpy(some_other_str, result);
    puts(some_other_str);
  }
}
Lundin
  • 195,001
  • 40
  • 254
  • 396
0

If you just want to extract naively the remaining string after the first occurence of "//" you probably need something like this:

#include <stdio.h>
#include <string.h>

int main()
{
  const char *text = "this is a test //bread is great";
  const char* commentstart = strstr(text, "//");

  char comment[100] = { 0 }; // naively assume comments are shorter then 99 chars

  if (commentstart != NULL)
  {
    strcpy(comment, commentstart + 2);
  }

  printf("Comment = \"%s\"", comment);
}

Disclaimers:

  • This is untested simple code that shows a possible approach. There is no error checking whatsoever, especially if the comment is longer than 99 chars, there will be a buffer overflow.
  • This code is absolutely not suitable for extracting comments from real life C code.
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115