-2

Here is a small snipped of code.

int len(char *str)
{
    int count = 0;
    while(*str++)
        count++;
    return count;
}

Now few days back I found a code which was able to this in one line. Basically there was some one line code in while's condition and that took care of finding the length of the string and returning it. It was dirty code? Yes, but it was interesting so I thought I would try to use it, but I can't seem to remember what it was exactly. So is there a way to do this in one line of code?

Edit: Sorry for not mentioning the fact that no library calls like strlen. This function must be self-contained.

Jack Bary
  • 63
  • 1
  • 6
  • 13
    `size_t length = strlen(str);`. – DaV Aug 19 '16 at 08:59
  • 1
    Remove new lines and you can do it one line too! `int len(char*str){int count=0;while(*str++)count++;return count;}` Now you just need to figure out what you achieved with that. As it turns out, this only makes the code uglier - the resulting binary will be the same. _There is almost never any relation between lines of code and program efficiency_. – Lundin Aug 19 '16 at 09:03
  • 5
    Better suited for code golf stack exchange. – Daniel Jour Aug 19 '16 at 09:04
  • Premise of this question seems impossible, as you cannot have `return` in `while` condition expression. – user694733 Aug 19 '16 at 09:05
  • 1
    `return strchr(str, 0) - str;` – BLUEPIXY Aug 19 '16 at 09:23
  • @Bob__ Would be nice, but you cannot have a declaration at that point, can you? – Daniel Jour Aug 19 '16 at 09:26
  • @DanielJour No, you can't, and even if you could, the compiler could not separate the following `while` from another id decl-part. – WhozCraig Aug 19 '16 at 09:28
  • 3
    Please people, close this question already. It's unclear and too broad. And we don't need this many subpar `strlen` clones. Somebody might even think that it's good idea to use them! – user694733 Aug 19 '16 at 09:30
  • 1
    _1:if (*str++ && ++count) goto _1; – DeftlyHacked Aug 19 '16 at 10:37
  • Sorry to all, I should have explicitly stated that no library calls are required. But I do remember that I saw a youtube video which literally showed how to do it in one line without library calls. I just can't find it. – Jack Bary Aug 19 '16 at 10:37

3 Answers3

5

If you do not want to use strlen and for, consider recursion.

E.g.:

int len(const char * s)
{
    return (*s) ? (1 + len(s+1)) : 0;
}

Explanation:

Ternary operator (?:) is used instead of if and if data reached with pointer s (the first letter) is not \0 we count that character by +1 to len of the rest of string.

Note:

Function fails when s point to nothing (i.e. s == NULL) or "string" is not zero-terminated.

VolAnd
  • 6,367
  • 3
  • 25
  • 43
  • 1
    Congratulations, you did not only manage to completely kill readability, but you also killed any hint of performance. Code doesn't get much worse than this. – Lundin Aug 19 '16 at 09:11
  • 1
    @Lundin Yes, I would have never written this in my program, but the question is.... and this is my answer – VolAnd Aug 19 '16 at 09:15
  • 2
    Well anyway, the amount of silly answers is a clear indication that this should be asked on Code Golf instead. – Lundin Aug 19 '16 at 09:16
3

You can use strlen(const char *s)


Of course you could use the onliner:

int len(char*str){int count=0;while(*str++)count++;return count;}

But it will only work correctly if your string is null terminated, if it's not, you'll find some unexpected results...

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
  • obviously using another function is not what the OP asked for – Ohad Eytan Aug 19 '16 at 09:01
  • 4
    @OhadEytan No, that's not obvious at all. Using `strlen` is the correct answer in real-world programming. – Lundin Aug 19 '16 at 09:05
  • 2
    Not what was asked for, but the only real (= to be considered in any more or less serious code) option. +1 – Daniel Jour Aug 19 '16 at 09:06
  • anything can be done with one-liner if that line is calling another function. – Ohad Eytan Aug 19 '16 at 09:11
  • 5
    If it's not null-terminated, it's not a string. – DevSolar Aug 19 '16 at 09:11
  • 3
    @ThomasAyoub It's called specification. If your library documents that "this function will take a null-terminated string-..." and some low life tosses in a raw character array as parameter, the library is not obliged to deal with the error nor is it responsible for it. – Lundin Aug 19 '16 at 09:22
  • 2
    @ThomasAyoub: The OP is about the length of a **string**. A string is (in section 7.1.1 of the standard) *defined* as "null-terminated". You pass something not null-terminated to *any* of the standard library functions operating on strings (e.g. `strlen()`), you get undefined behaviour, and it's your fault, not the library's. Same goes for every single third-party library out there. Aside from that... let's say you get a non-terminated array as a parameter to your function. How *would* you go about determining that array's length? (Hint: You cannot.) – DevSolar Aug 19 '16 at 09:51
1

The only real solution is to use strlen.

That said, how about:

char *s = str; while(*s++);
size_t l = (s - str) - 1;

I'm not putting it in one line because removing the line breaks isn't that difficult and the compiler doesn't care.

Daniel Jour
  • 15,896
  • 2
  • 36
  • 63