0

I'm at the very beginning of learning to code (specifically C). While writing a function which is counting the chars of an array for studying purposes, I was questioning myself (and I'm pretty sure about it), if there is the possibility to simplify this iteration:

int stringlength(char* s)
{
    int i = 0;

    while (s != NULL && *(s + i) != '\0')
        i++;

    return i;
}

I would like to keep the i++ within the iteration itself (for loop?).

I'm appreciating any hint you guys got for me. If you find something question unrelated which I'm doing wrong - please let me know.

Jongware
  • 22,200
  • 8
  • 54
  • 100
hi im vinzent
  • 163
  • 2
  • 15
  • 2
    You can extract the check for `s != NULL` into an `if` condition at the beginning at the function – UnholySheep Dec 06 '16 at 10:46
  • Note that functions whose names start with `strx` where `x` is a lower-case letter are a bad idea, that whole space of names is reserved for the standard library. This particular function should be safe, though since we already have `strlen()`. :) – unwind Dec 06 '16 at 11:14
  • Thanks for the hints :) Thinking about extracting {s != NULL}, am I extracting this check only for performance reasons (not checking for the same every char of the array)? – hi im vinzent Dec 06 '16 at 11:53

4 Answers4

2
if(s == NULL)
    return 0;
int i;
for(i = 0; s[i] != '\0'; i++);

This should solve your purpose, I believe.

EDIT: Adding a NULL check for the input char* since NULL could be a possible value.

user007
  • 2,156
  • 2
  • 20
  • 35
2
int stringlength(char* s)
{
    if (!s) 
       return 0;
    int i = -1;
    while(s[++i]!='\0');
    return i;
}
Sumit Gemini
  • 1,836
  • 1
  • 15
  • 19
0

I tested strlen(NULL) of glibc, it causes Segment Fault and I think it is reasonable to cause Segment Fault. So here is my implementation:

#include <assert.h>

int stringlength(const char* s) {
    assert(s != NULL);
    int i;
    for (i = 0; s[i]; ++i);

    return i;
}

Note: s is const char *, but not char *.

CharlesLiuChina
  • 261
  • 1
  • 16
-1

I suggest using pointers arithmetic.

First, handle the NULL case (empty string, hence you only get the null terminator). Otherwise, iterate on the pointer and increment the counter. Then, simply return that counter.

int stringlength(char* s)
{
   if (!s) return 0;
   int len = 0;

   while (*s++) len++;

   return len;
}

What's the magic with the (*s++) ? Well this is equivalent to *(s++) != 0. But because ++ has a higher precedence than *, the parenthesis are useless.

Also, you can have a look on this thread.

Community
  • 1
  • 1
Aif
  • 11,015
  • 1
  • 30
  • 44
  • Thanks a lot. Could you let me know why creating a copy of the source pointer is useful/necessary? Also, if you find some time to explain some more, how exactly is the condition of "while" working in this case? *cp++ is checking for the "next" char in the string, but why exactly is the condition (*cp++) finding to an end if I'm not checking for "\0"? – hi im vinzent Dec 06 '16 at 11:45
  • @hiimvinzent I will edit the response. The copy is not needed here, indeed. – Aif Dec 06 '16 at 11:51
  • @YassineHoussni: did you try it? – Aif Dec 06 '16 at 13:05
  • I did compile it without copying but using this iteration and it works fine for me. As I'm only checking for the value and not changing it, I assume copying is not necessary. – hi im vinzent Dec 06 '16 at 14:04