Search code examples
cstringfindsubstringsubstr

C string functions


In C, how do I extract the first n characters from a string until I find a space in the string? Essentially, which C function will find the position of the next space for me and which C function will give me a substring? I am thinking in terms of C++. such as:

string str = "Help me Please!";
int blankPos = str.find(' ');
str.substr(0, blankPos);

Thanks,


Solution

    1. Use strchr to find the space.
    2. Allocate a new char buffer to hold the substring.
    3. Copy the substring into the buffer with memcpy.