tags:

views:

182

answers:

6

I need to count the number of characters in the string without using the strlen function.

int mystrlen(char *s)
{
    char s[]=”program”;
    int counter = 0 ;
    int i;
    for (i = 0; i < s[i]!= 0; i++)
    { 
     counter++;
    }

    printf( "The number of characters is %d", counter);

    return 0;
A: 

What's wrong with doing something very similar to the strlen implementation: http://bfos.scottnz.com/trac/browser/trunk/lib/c/newlib/string/strlen.c?rev=1320?

DaveS
+1  A: 

assuming s is null terminated

int mystrlen(char *s)
{
    int i =0;
    while (*s++) i++;
    return i;
}
Alon
-1 for posting code in response to homework question
qrdl
It will segfault upon receiving a null
rmn
@rmn: you are correct, but note that strlen will also expect a none null pointer
Alon
A: 

I'll not give the direct code as it is a homework, I can suggest couple of things here. First your variable names are same i.e. you have a char* s and char s[]="program";. I believe the second one is unnecessary. Secondly, take a close look at your for loop, does the condition look correct to you?

Naveen
+2  A: 

Your for loop is a little weird, I think:

for (i = 0; i < s[i]!= 0; i++)

"loop while i is less than the character at the i-th position not equals zero"? Remember that the loop condition does not have to look like "i < foo". You can write

for (i = 0; s[i] != 0; i++)

perfectly fine; this will loop while the character at the i-th position is non-zero.

Your counter variable is superfluous in this case, though, as i is already a counter too.

Joey
Note: Yes, this can be made a whole lot more "elegant" or short using pointer arithmetic and whatnot but I don't believe it will really help anyone but an experienced C hacker to provide a clever and tricky one-liner here.
Joey
+1  A: 

What you need to do is very similar to what strlen does, using a loop like:

  while (data && *data++ != '\0')

Supposing the data is the string itself.

Notice the null safety, which is missing in previous answers.

rmn
A: 

If the string is Null-terminated, following should be faster.

size_t len(const char* s){
    if(!s) return 0;
    char* ofs=s;
    while(*s++);
    return s-ofs-1;
}

Inside while loop there is only one counter ++ , and one checking part *s

If compare to while (*s++) i++;, which has 2 counters, 1 checking part, so more processes.

Or, If you want even faster one, check 4 bytes at a time, check following implementation.

http://www.stdlib.net/~colmmacc/strlen.c.html

S.Mark
you will segfault upon receiving a null.
rmn
thx added a check on top.
S.Mark
Um, hold on, even ` if(!*s) return 0;` is not there, it should work, because lets say `*s=0`, while loop cannot continue, s just incremented, `s-ofs-1` will be 0, so it should be fine.
S.Mark
Your check for a NULL pointer is currently wrong, it needs to drop the asterisk.
unwind
Also, strlen() takes a const char * and returns size_t.
unwind
Ah, I got what rmn mean now, thx unwind, fixed, and fixed.
S.Mark