Comp sci: C programming: strpbrk

Self-tutoring about C programming: the tutor mentions how to get the suffix of a string.

People who arrive at C from other languages, possibly JavaScript or C# or Python, might notice that string functions seem a little less represented in C. Small wonder: after all, strings don’t even exist in C; rather, they’re character arrays. (Yet, ironically, the string functions in C are from <string.h>, its own self-titled library.)

C string functions seem tuned to get a prefix of a string, but not the suffix. Yet, with a pointer, the suffix is easy from the prefix.

Let’s imagine you have the string

“She doesn’t believe in intermittent fasting because….”

You want, from it, to extract everything after “doesn’t”. Here’s how you might:

char sentence[]=”She doesn’t believe in intermittent fasting because….”;
char *p;
p=strpbrk(sentence, “'”);//the apostrophe is the target
p+=strlen(“'t “); //strlen: string length

The output, hopefully:

believe in intermittent fasting because….


Kernighan, Brian and Dennis Ritchie. The C Programming Language. New Jersey: Prentice Hall, 1988.

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Leave a Reply