The string functions in C are as follows

  • Get the length of the string
    • strlen
  • A string function of unlimited length
    • strcpy
    • strcat
    • strcmp
  • String function with limited length
    • strncpy
    • strncat
    • strncmp
  • String lookup
    • strstr
    • strtok
  • Error message report
    • strerror

Now how do you implement them

Get the length of the string

strlen

Let’s see what the documentation says. Right

Strlen document

size_t strlen ( const char * str );
Copy the code

Get string length

Get the length of the string

Returns the length of the C string str.

Returns the length of the C string STR

The length of a C string is determined by the terminating null-character: A C string is as long as the number of characters between the beginning of the string and the terminating null character (without including the terminating null character itself).

C String length is determined by ‘\0’, that is, from the first part of the string, the length is calculated as soon as ‘\0’ is encountered (excluding ‘\0’).

This should not be confused with the size of the array that holds the string. For example:

Don’t get confused about the size of the array you’re creating, like this

char mystr[100] ="test string";
Copy the code

defines an array of characters with a size of 100 chars, but the C string with which mystr has been initialized has a length of only 11 characters. Therefore, while sizeof(mystr) evaluates to 100, strlen(mystr) returns 11.

Define an array mystr of size 100, and mystr has been initialized to a string of length 11. So sizeof(mystr) returns 100, and strlen(mystr) returns 11.

In summary, we can see that

  1. The string is already terminated by ‘\0’. Strlen returns the number of characters that appear before ‘\0’ in the string (excluding ‘\0’).
  2. This function recognizes only ‘\0’, and the argument to the string must end with ‘\0’.
  3. Note that the return value of the function is size_t, which is unsigned

implementation

There are several implementations of the strlen function.

Such as

  1. Counter method
  2. recursive
  3. Pointer – pointer

Let’s do it one by one.

1. Counter: Use a variable to record -count

It is a good habit to assert that Pointers are not null

int my_strlen(char* str) 
{
    int count = 0; assert(str ! =NULL);
    while(*str ! ='\ 0') // while (*str)
    {
        count++;
        str++;
    }
    return count;
}
Copy the code

Keep looking for ‘\0’, count++, string ++ when * STR is not ‘\0’, until it stops at ‘\0’, and returns count as the length.

2. Recursively

It is a good habit to assert that Pointers are not null

int my_strlen(char* str)
{ assert(str ! =NULL);
    char* p = str;
    while(*p == '\ 0')
    {
        return 0;
    }
    return 1 + my_strlen(p + 1);
}
Copy the code

For example, the STR address passed in is 1000

So 1 + my_strlen(p + 1), p + 1, the offset is 1001, and so on.

1 + 1 + my_strlen(p + 1)

1 + 1 + 1 + my_strlen(p + 1)

1 + 1 + 1 + 1 + my_strlen(p + 1)

.

1 + 1 + 1 + 1 +… + 0

And then you get the length.

3. Pointer – Pointer

It is a good habit to assert that Pointers are not null

int my_strlen(char* str) 
{ assert(str ! =NULL);
    char* p = str;
    while(*p ! ='\ 0') 
    {
        p++;
    }
    return p - str;
}
Copy the code

Assign the address of pointer STR to a new pointer p. STR acts as a pointer to the starting address, leaving it unchanged.

P ++, then proceed to the next character, and so on until p finds ‘\0’, and returns the length by subtracting the current p pointer from STR.