-9

I'm going to have a test in next week. And in the sheet they say that "You can't use strlen() in test" How can I make it.

Can use only ONLY!!!

chivacalo
  • 1
  • 1
  • 2
    Read up on what strlen does. It should be easy to figure out a simple implementation from that. – juanchopanza Oct 11 '14 at 12:47
  • 1
    A lot of 'experienced' developers seem to now know what strlen() does. It looks for a null in a raly big array or at the end of an incrementing pointer. – Martin James Oct 11 '14 at 12:55
  • 1
    Could you be barking up the wrong tree? If the test says that you can't use `strlen`, that might be an indicator that you don't need it in the solution rather than an invitation to roll your own. – M Oehm Oct 11 '14 at 13:32

3 Answers3

2
size_t my_strlen(const char *s){
    const char *p=s;
    while(*p)
        ++p;
    return p-s;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • While tecnically perfect, size_t is defined in stddef.h and might therfore violate the specified requirement to only use stdio.h – Ron Pinkas Oct 11 '14 at 13:20
  • 2
    @RonPinkas It seems to be no problem, for example, from being used in functions such as `fread` (in `size_t fread(void * restrict ptr,size_t size,size_t nmemb,FILE * restrict stream);`). – BLUEPIXY Oct 11 '14 at 13:24
  • Indeed. Thanks for the correction. :-) – Ron Pinkas Oct 11 '14 at 13:26
-1

Try something like this.

#include<stdio.h>

int strlen(const char* temp)
{
    int i=0;
    while(*(temp++)) ++i;
    return i;
}

int main()
{
    char *text = "Hello World";
    printf("%d",strlen(text));
    return 1;
}
Inquisitive
  • 475
  • 1
  • 5
  • 13
-2
unsigned long mystrlen( const char *szString )
{
   unsigned long ulLen = 0;

   While( szString[0]){ szString++; ulLen++;}

   return ulLen;
}
Ron Pinkas
  • 357
  • 2
  • 10