2

I'm confused how strnlen could work in C++ with strings that weren't null terminated, as I 'm not sure how it computes the size.

The strlen() implementation is easy:

size_t strlen(char *s)
{
    size_t sz;
    while(*s++ != '\0')
    {
        ++sz;
    }
    return sz;
}

But how would you implement strnlen()?

4
  • 4
    Did you read the documentation?
    – SLaks
    Feb 3, 2014 at 19:33
  • The strnlen(s, maxlen) function returns strlen(s), if that is less than maxlen, or maxlen if there is no null byte ('\0') among the first maxlen bytes pointed to by s.
    – raina77ow
    Feb 3, 2014 at 19:34
  • yes someone is really frustrated I think ;) Feb 3, 2014 at 19:46
  • Do you want strlen or strnlen? These are not the same.
    – drescherjm
    Feb 3, 2014 at 19:48

5 Answers 5

10

A little shorter than πάντα ῥεῖ's answer:

size_t strnlen(const char * s, size_t len) {
    size_t i = 0;
    for ( ; i < len && s[i] != '\0'; ++i);
    return i;
}

EDIT:

To clarify for future readers and possible downvoters, when I wrote this answer, my code was in fact shorter. Now it's not anymore, because the other post was edited. I don't think that this should be a reason to downvote me.

12
  • 1
    May be because you could even omit the {} (I didn't downvote) Feb 3, 2014 at 19:45
  • 1
    lol, two seconds after deleting the curly brackets, I got 2 upvotes. EDIT: And now it's down again, it's like a rollercoaster. Maybe I shouldn't answer in the future if there are already sufficient answers. Feb 3, 2014 at 19:46
  • 1
    @admdrew At least it refers to my answer, which was given a few minutes faster (being also correct at this time). You can always optimize later, as soon you've got everything right ;) ... Feb 3, 2014 at 19:59
  • 1
    Well, to make things clear and to end this "race", I gave you an upvote, so we can go on with our lives. ;-) Feb 3, 2014 at 20:08
  • 1
    After the serial downvotes, I have a feeling that he won't accept any of the answers ... Feb 3, 2014 at 20:12
8

But how would you implement strnlen()?

Like this:

size_t strnlen(const char *s, size_t max_len)
{
    size_t i = 0;
    for(; (i < max_len) && s[i]; ++i);
    return i;
}
2
  • 1
    Looks like gartenriese is still beating you in a little code golf ;)
    – admdrew
    Feb 3, 2014 at 19:53
  • 2
    You have the test the wrong way around. You shouldn't dereference s[i] beyond 'max_len'. It may not point to anything.
    – user207421
    Feb 3, 2014 at 20:08
2

strnlen == min(n, strlen)

e.g. instead of

while(*s++ != '\0')

use

for(int i = 0; i < n && *s++ != '\0'; i++)

or any equivalent.

In the future, feel free to read the source yourself, which I just found by googling "strnlen source code."

2

Your code is invalid because variable sz was not initialized

size_t sz;

I think it is simply a typo.

Also the parameter should be defined as const char *s

strnlen is not a C++ standard function. It is defined only in C. it could be defined the following way

size_t strnlen( const char *s, size_t n )
{
   size_t i = 0;

   while ( i < n && s[i] ) ++i;

   return i;
}

In C++ it could be declared having a default argument

size_t strnlen( const char *s, size_t n = -1 );
0
2

But how would you implement strnlen?

You really don't need to in C++, just use std::find like so.

auto length = std::find(s, s + max_len, 0) - s;

Or if you want to wrap it up in a template function so it also works with more than just single byte characters.

template<type Type>
size_t strnlen(const Type* s, size_t max_len)
{
    return std::find(s, s + max_len, 0) - s;
}

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.