'Rewriting strlen() in C [closed]

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!



Solution 1:[1]

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;
}

Solution 2:[2]

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;
}

Solution 3:[3]

There's no need for an int counter.

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

Solution 4:[4]

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

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

Solution 5:[5]

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;

}

Solution 6:[6]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 autistic
Solution 2 John3136
Solution 3
Solution 4 Déjà vu
Solution 5 sv1
Solution 6 kris123456