Search code examples
cstringstrcmpstrcpystrcat

How does this basic reimplementation of strlen() find the length?


My professor showed us this sample implementation of a function to find the length of a string, saying that this is how you would do it without actually using the strlen() function:

int length(char word)
{
    int i;
    for (i = 0; word[i]!='\0'; i++)
    {
        {

        }
    }
    return i;
 }

She said it would be in our best interest for us to do similarly for strcat() , strcmp(), and strcpy(), to understand how the functions actually work.

What is going on in this function? Is there anything I can learn from this example to help me reimplement the other three?


Solution

  • You seem to be asking multiple questions, I'll attempt to answer your first one.

    Your function above is not valid, it will not compile, or work.

    Here's an implementation of strlen.

    size_t strlen(char s[])
    {
        size_t i;
    
        i = 0;
        while (s[i]= != '\0') //loop over the array, if null is seen, break
            ++i;
        return i; // return length (without null)
    
    }
    

    In regards to the explanation, strings in C are represented as an array of characters. There has to a logical way to denote the end of a string, which in our case is the null character \0. Be careful with this, strings require n+1 storage, most implementations of strlen return the length of the string excluding the null character.

    You may be wondering what \0 represents, in C the null character represents a character constant, the value in ASCII is 0. You could write 0, instead \0 is added to emphasize the nature of the string.

    Again, another implementation of strlen using pointer arithmetic

    size_t strlen(char *s)
    {
        char *p = s;
        while (*p != '\0')
            ++p;
        return p - s; //use ptrdiff_t or size_t?
    }
    

    Also, strcat for good measure.

    void strcat(char s[], char t[])
    {
        int j, k;
    
        j = k = 0;
        while (s[j] != '\0') // are we at the end of s? 
            ++i;
        while ((s[j++] = t[k++]) != '\0') // copy t to s 
            ; // null statement
    }
    

    the library implementations returns a pointer to the string, not void; see man 3 strcat

    In strcat, a character is copied (sequentially) from t to the end of s. A common C idiom, is to increment the stepper variable in the loop. As each character is copied, the stepper variable is incremented to be in place for the next character.

    A void return value may be used, due the internal representation of arrays in C as pointers (return by reference.)

    A devastating bug in this implementation is we assume s is big enough to hold all of s + t + 1. In the interest of brevity, I ignored this issue.