2

I know C is purposefully bare-bones, but I'm curious as to why something as commonplace as a substring function is not included in <string.h>.

Is it that there is not one "right enough" way to do it? Too many domain specific requirements? Can anyone shed any light?

BTW, this is the substring function I came up with after a bit of research. Edit: I made a few updates based on comments.

void substr (char *outStr, const char *inpStr, int startPos, size_t strLen) {
    /* Cannot do anything with NULL. */
    if (inpStr == NULL || outStr == NULL) return;

    size_t len = strlen (inpStr);

    /* All negative positions to go from end, and cannot
    start before start of string, force to start. */
    if (startPos < 0) {
        startPos = len + startPos;
    }
    if (startPos < 0) {
        startPos = 0;
    }

    /* Force negative lengths to zero and cannot
    start after end of string, force to end. */
    if ((size_t)startPos > len) {
        startPos = len;
    }

    len = strlen (&inpStr[startPos]);
    /* Adjust length if source string too short. */
    if (strLen > len) {
        strLen = len;
    }

    /* Copy string section */
    memcpy(outStr, inpStr+startPos, strLen);
    outStr[strLen] = '\0';
}

Edit: Based on a comment from r I also came up with this one liner. You're on your own for checks though!

#define substr(dest, src, startPos, strLen) snprintf(dest, BUFF_SIZE, "%.*s", strLen, src+startPos)
13
  • 3
    Wouldn't strncpy let you do the same thing?
    – Tom Zych
    Sep 13, 2011 at 17:54
  • 4
    Any question that asks "Why does the X standard not include feature Y" are tricky to answer definitively. Sep 13, 2011 at 17:55
  • 1
    strncpy doesn't do quite what you think it does. I'd use memcpy here personally. (Also, the size_t type is preferred for array indices and sizes.)
    – Chris Lutz
    Sep 13, 2011 at 17:57
  • 2
    If you ask 10 C programmers for a specification for a generic substring function you're likely to get 10 different answers. Should it allocate memory? Should it allow negative indexes? Do we need a substringn function that also takes the length of the destination buffer? etc.
    – user786653
    Sep 13, 2011 at 18:08
  • 2
    @Tom: strncpyis very unlikely to be the right answer to any particular problem. Sep 13, 2011 at 18:16

6 Answers 6

7

Basic standard library functions don't burden themselves with excessive expensive safety checks, leaving them to the user. Most of the safety checks you carry out in your implementation are of expensive kind: totally unacceptable in such a basic library function. This is C, not Java.

Once you get some checks out of the picture, the "substrung" function boils down to ordinary strlcpy. I.e ignoring the safety check on startPos, all you need to do is

char *substr(const char *inpStr, char *outStr, size_t startPos, size_t strLen) {
  strlcpy(outStr, inpStr + startPos, strLen);
  return outStr;
}

While strlcpy is not a part of the standard library, but it can be crudely replaced by a [misused] strncpy. Again, ignoring the safety check on startPos, all you need to do is

char *substr(const char *inpStr, char *outStr, size_t startPos, size_t strLen) {
  strncpy(outStr, inpStr + startPos, strLen);
  outStr[strLen] = '\0';
  return outStr;
}

Ironically, in your code strncpy is misused in the very same way. On top of that, many of your safety checks are the direct consequence of your choosing a signed type (int) to represent indices, while proper type would be an unsigned one (size_t).

8
  • What would the proper way to use it be? Sep 13, 2011 at 17:58
  • I'd rather see memcpy here than strncpy.
    – Chris Lutz
    Sep 13, 2011 at 18:01
  • @Chris Lutz: memcpy will not stop at terminating \0 in the input. To use memcpy you have to calculate strlen first. I agree that strncpy is totally misused here, but I was aiming for brevity. Sep 13, 2011 at 18:04
  • @Derek Springer: strncpy is a function that converts C-string to fixed-width strings. That's what it is used for. Using it for "safe" string copying is a crime against programming. stackoverflow.com/questions/2114896/… Sep 13, 2011 at 18:08
  • 1
    I like how the void function returns a char *. (I actually thought that function would be better if it returned it anyway for initialization purposes)
    – Joe
    Sep 13, 2011 at 18:15
3

Perhaps because it's a one-liner:

snprintf(dest, dest_size, "%.*s", sub_len, src+sub_start);
5
  • Do you know the relative efficiency of snprintf over memcpy? Sep 13, 2011 at 18:59
  • 3
    @Derek Springer: In string handling you should be more worried about safety than efficiency. If you, after lengthy and sober profiling, determine that using snprintf is too slow in your application, you are probably better off trying to avoid a direct substring operation rather than using memcpy instead of snprintf.
    – user786653
    Sep 13, 2011 at 19:14
  • Does snprintf guarantee null termination on the destination buffer?
    – selbie
    Sep 13, 2011 at 19:32
  • @Selbie: Yes it does--I was wondering the same thing. From the man page: The functions snprintf() and vsnprintf() write at most size bytes (including the trailing null byte ('\0')) to str. Sep 13, 2011 at 19:39
  • Yes snprintf always terminates, unless dest_size is zero in which case it does not write anything. Sep 13, 2011 at 20:19
2

You DO have strcpy and strncpy. Aren't enough for you? With strcpy you can simulate the substring from character to end, with strncpy you can simulate the substring from character for a number of characters (you only need to remember to add the \0 at the end of the string). strncpy is even better than the C# equivalent, because you can overshoot the length of the substring and it won't throw an error (if you have allocated enough space in dest, you can do strncpy(dest, src, 1000) even if src is long 1. In C# you can't.) As written in the comment, you can even use memcpy, but remember to always add a \0 at the end of the string, and you must know how many characters you are copying (so you must know exactly the length of the src substring) AND it's a little more complex to use if a day you want to refactor your code to use wchar_t AND it's not type-safe (because it accepts void* instead of char*). All this in exchange for a little more speed over strncpy

12
  • 3
    strncpy isn't "a safe strcpy." Be careful around code that appears to use it as such.
    – Chris Lutz
    Sep 13, 2011 at 17:59
  • @Chris If you always terminate it with a "bonus \0", I don't see any problem at using it. But yes, the fact that you have t pass to it bufferlength-1 IS a big problem :-) But in the end... More money for me :-) :-)
    – xanatos
    Sep 13, 2011 at 18:03
  • I prefer to use memcpy when I can. It doesn't perform the extra (often unnecessary) work of checking for nul-termination, or filling in unused space with zeroes, and you always know how much data it copies.
    – Chris Lutz
    Sep 13, 2011 at 18:06
  • 3
    @xanatos: But what about the needless padding? strncpy was designed for a very specific purpose, one that we rarely run into these days. IMHO it shouldn't be in the standard library. Sep 13, 2011 at 18:21
  • 1
    @Chris, I think you should replace "to avoid floating-point arithmetic" with "to be faster than floating-point arithmetic". If the reason for avoiding floating point is anything else (like wanting reproducible bit-exact answers) then it's probably a very legitimate concern, not premature optimization. Sep 13, 2011 at 18:35
0

In C you have a function that returns a subset of symbols from a string via pointers: strstr.

char *ptr;
char string1[] = "Hello World";
char string2[] = "World";

ptr = strstr(string1, string2)

*ptr will be pointing to the first character occurrence.

BTW you did not write a function but a procedure, ANSI string functions: string.h

1
  • 1
    This is the same comment selbie originally made--I don't think that's exactly the same thing: strstr returns a pointer to the first occurrence of str2 in str1--helpful only if you know exactly what you are looking for. Sep 13, 2011 at 18:19
0

Here's a lighter weight version of what you want. Avoids the redundant strlen calls and guarantees null termination on the destination buffer (something strncpy won't do).

void substr(char* pszSrc, int start, int N, char* pszDst, int lenDest)
{
    const char* psz = pszSrc + start;
    int x = 0;

    while ((x < N) && (x < lenDest))
    {
        char ch = psz[x];
        pszDst[x] = ch;
        x++;
        if (ch == '\0')
        {
           return;
        }
    }

    // guarantee null termination
    if (x > 0)
    {    
        pszDest[x-1] = 0;
    }
}

Example:
char *pszLongString = "This is a long string";
char szSub[10];
substr(pszLongString, 0, 4, szSub, 10); // copies "long" into szSub and includes the null char

So while there isn't a formal substring function in C, C++ string classes usually have such a method:

#include <string>
...
std::string str;
std::string strSub;

str = "This is a long string";

strSub = str.substr(10, 4); // "long"

printf("%s\n", strSub.c_str());
4
  • I don't think that's exactly the same thing: strstr returns a pointer to the first occurrence of str2 in str1--helpful only if you know exactly what you are looking for. What I'm talking about is returning just "burg" from "hamburger." Sep 13, 2011 at 17:56
  • Your new function takes 5 arguments, but you only call it with 4.
    – Chris Lutz
    Sep 13, 2011 at 18:38
  • @Oli - that's rather draconian of you to downvote since I gave both a "C" and "C++" answer.
    – selbie
    Sep 13, 2011 at 19:30
  • @selbie - At the time of his downvote, you only had a C++ answer.
    – Chris Lutz
    Sep 13, 2011 at 19:32
-1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const char* substr(const char *string, size_t from, size_t to);

int main(int argc, char *argv[])
{
    char *string = argv[1];

    const char *substring = substr(string,6,80);

    printf("string is [%s] substring is [%s]\n",string,substring);

    return 0;
}

const char* substr(const char *string, size_t from, size_t to)
{
    if (to <= from) 
        return NULL;

    if (from >= to)
        return NULL;

    if (string == NULL)
        return NULL;

    if (strlen(string) == 0)
        return NULL;

    if (from < 0)
        from = 0;

    if (to > strlen(string))
        to = strlen(string);

    char *substring = malloc(sizeof(char) * ((to-from)+1));

    size_t index;

    for (index = 0; from < to; from++, index++)
        substring[index] = string[from];

    substring[index] = '\0';

    return substring;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.