C: Get substring before a certain char

c, char, string

Solution

Just put a 0 at the place of the slash

#include <string.h> /* for strchr() */

char address[] = "10.10.10.10/10";
char *p = strchr(address, '/');
if (!p)
{
    /* deal with error: / not present" */
    ;
}
else
{
   *p = 0;
}

I don't know if this works in C++

Problem

For example, I have this string: `10.10.10.10/16` and I want to remove the mask from that IP and get: `10.10.10.10` How could this be done?

Original source

Related problems