Remove text from string after a certain character in C

William Fernandes picture William Fernandes · Jun 20, 2016 · Viewed 7k times · Source

I am reading lines from a file, the lines look like this:

89f81a03eb30a03c8708dde38cf:000391716

The thing is: I want to remove everything after the : (including the :). I tried everything I could find online but they seem to use const char and the lines are char pointers.

Answer

David Ranieri picture David Ranieri · Jun 20, 2016

You can use strchr:

char str[] = "89f81a03eb30a03c8708dde38cf:000391716";
char *ptr;

ptr = strchr(str, ':');
if (ptr != NULL) {
    *ptr = '\0';
}