Rewriting strlen() in C

12.5k views Asked by At

if anyone has ever wanted to try and rewrite a string function in C, would this code work to replace the standard strlen() function? I have not been able to run this code because I am sitting in my car typing it on my iPhone.

int strlen(char * s) {
      int i = 0, sum = 0;
      char c = s[0];

      while(c != '\0') {
            sum++;
            c = s[++i];
      }
      return sum;
}

You feedback is so much appreciated!

6

There are 6 answers

0
sv1 On

Here are different methods to write down code for strlen:

Without Using Pointers:

int mystrlen(char s[]) {

int i = 0;

while (s[i]!='\0')
 {
  i++;
 }

return i;

}

With Pointers:

int strlength(char const *s) {

int l=0;                                                    

while(*s1!='\0')                                      
{
  l++;
  s++;
}

return l;                                              

}

Without Counter:

int my_strlen(char const *s) {

char *p=s;

while(*p!='\0')
     p++;

return(p-s);

}

Using Recursion :

int mystrlen(char const *s) {

 if(*s==0) 
      return 0;

 return mystrlen(s+1)+1;

}

3
John3136 On

Looks like it would work, but you don't need 2 integers, so it's overly complex. [] lookup might be slow - pointer manipulation might be faster.

int myStrlen(char *s)
{
    int len = 0;
    while(*s != 0) {
        s++;
        len++;
    }
    return len;
}
0
Déjà vu On

It should work. While the function can be optimized. Something like

 int my_strlen(char *s) {
    int sum = 0;
    while (*s++) sum++;
    return sum;
 }
0
doog abides On

There's no need for an int counter.

size_t myStrlen(char *s)
{
 char *e=s;
 while(*e!='\0')e++;
 return e-s;
}
4
kris123456 On

The actual functionality is much more than what you have written.

  • Consider a scenario, where you have allocated a size of 100 to your pointer. And you forgot to add the '\0' as the last char for your array. How are you going to handle this situation? You cannot keep on incrementing the counter and keep looking for '\0'

  • Arrays have a max size. Your pointer string length function should also have a realistic length. you cannot keep on iterating the counter and look for '\0'. What if you enter another processes memory location and you start reading and writing to that application memory? You would end up messing multiple applications.

  • Depending on the type of compiler, it may or may not allocate memory sequentially. How are you going to handle this situation?

In short, you should work on malloc/new first. Understand it end to end and then try working on this.

2
Vlad from Moscow On

If you want to rewrite standard C function strlen then you should follow the declaration conventions of the function.

size_t strlen(const char *s);

First of all it should have return type size_t and the parameter shall have qualifier const that you could use the function with constant character arrays. Also such a name as sum is not very suitable for this function. In your function you have too many local variables.

I would write the function the following way

size_t strlen( const char *s ) 
{
    size_t n = 0;

    while ( s[n] ) ++n;

    return n;
}

The other way is to use only pointers. For example

size_t strlen( const char *s ) 
{
    const char *p = s;

    while ( *p ) ++p;

    return p - s;
}