C detailed string functions


But on the road ahead, don’t ask about your return date

It should be noted that to use the functions mentioned below, you must include the header file

Article directory

      • strlen
      • strcpy
        • strncpy
      • strcat
        • strncat
      • strcmp
        • strncmp
      • strstr
      • strtok
      • strerror
      • String case conversion
        • strupr
        • strlwr
      • memcpy
      • memmove
      • memcmp

strlen

Find the length of a string

  • Function parameter: string pointer
  • Function function: Pass in a string pointer. The string ends with \0. The returned type size_t is actually unsigned int. The function returns the ** in the string. The number of characters in front of ‘\0’ **, the incoming string pointer is modified with const *str to prevent the function from accidentally modifying the incoming string.
  • Return value type: unsigned integer, that is, the number of characters before the end mark

Function demonstration

End the demonstration when ‘\0’ is found.

Simulation implementation
Write according to function function and return value
Expand one stroke

unsigned int _strlen(const char *arr)
{<!-- -->
int count = 0;//count
while(*arr)
{<!-- -->
count + + ;
arr + + ;//Character pointer moves backward
}
}

strcpy

  • Function parameters: one is the destination and the other is the source.
  • Function: Copy the source string to the target space.
  • Return value: Returns the address of the first string after copying is completed.

Notice:
1. The source string must end with ‘\0’. Can’t do this

char arr[4]={<!-- -->'a','b','c','d'}

?If the second parameter is such a string without the end mark ‘\0’, then the function will always look backward during implementation, and an out-of-bounds access will cause an error.
2. The function will also copy the end mark ‘\0’ of the source string.
Can be debugged and observed

?When printing, it ends when it encounters ‘\0’. Do not mistakenly think that this function clears the original content.
Simulation implementation (remember to include the header file assert.h)

char* _strcpy(char* dest, const char* src)
{<!-- -->
char* ret = dest;//Save the first address of dest
assert(dest != NULL);
assert(src != NULL);
while (*src!='\0')//Everything before \0 is OK
{<!-- -->
*dest = *src;
dest + + ;
src++;
}
*dest = *src;//\0 also come here
return ret;
}
strncpy

The difference from strcpy is that there is one more parameter.

?Copy num bytes. If the source string has no elements, change the bytes after dst to zero.
Function display:

strcat

  • Parameters: The parameters of this function are exactly the same as strcpy.
  • Function: strcpy copies the source string at the beginning of the target string, but strcat starts copying the source string to the target string at the end of the target string, that is, the end mark ‘\0’.
  • Return value: Same as strcpy, returns the pointer of the target string.

Function presentation:

Function simulation implementation:

char* _strcat(char* dest, const char* src)
{<!-- -->
char* ret = dest;
assert(dest != NULL);
assert(src != NULL);
while (*dest)
{<!-- -->
dest + + ;
}//Find the end of the target string
while (*dest + + = *src + + )
{<!-- -->
;
}
return ret;
}
strncat

The same difference as the previous strcpy and strncpy.

Function demonstration:

strcmp

String comparison function

  • Parameters: two, comparison between them
  • Function: Compare each character of two strings with ASCII code value one by one
  • If the first string is greater than the second string, a number greater than zero is returned.
  • If the first string is equal to the second string, 0 is returned.
  • If the first string is less than the second string, a number less than zero is returned.
  • Return value: As explained above.

Function function presentation
The ASCII of f precedes ‘\0’. Greater than returns 1 (a number greater than 0)

Returns zero if equal.

Less than returns -1 (a number less than 0)

Function simulation implementation

int _strcmp(const char* src, const char* dest)
{<!-- -->
int ret = 0;
assert(src != NULL);
assert(dest != NULL);
while (!(ret = *(unsigned char*)src - *(unsigned char*)dest) & amp; & amp; *dest)
{<!-- -->
+ + src;
+ + dest;
}
if (ret < 0)
{<!-- -->
return -1;
}
else if (ret > 0)
{<!-- -->
return 1;
}
else
{<!-- -->
return 0;
}
}
strncmp

Instead of comparing from beginning to end, we compare num characters.
Function demonstration:

strstr

  • Parameters: Pass in two character pointers.
  • Function: Find the first occurrence of str2 in str1.
  • Return value: Returns the pointer to the location found. If not found, NULL is returned.

Function function presentation

Function simulation implementation

char* _strstr(const char* str1, const char* str2)
{<!-- -->
char* cp = str1;
char* s1, * s2;
if (!*str2)
{<!-- -->
return str1;//If str2 is empty, return str1
}
while (*cp)
{<!-- -->
s1 = cp;
s2 = str2;//The incoming parameters cannot be modified
while (*s1 & amp; & amp; *s2 & amp; & amp; !(*s1 - *s2))
{<!-- -->
s1 + + ;
s2++;
}
if (!*s2)//If the loop is broken out and s2 is already '\0'.
{<!-- -->
return cp;
}
cp + + ;
}
return NULL;
}

strtok

  • Parameters: The first parameter is the target string, the second parameter is the delimiter set
  • Function: The first parameter, the target string, contains 0 or more strings separated by delimiters.
    ?strtok will find a mark in str, change the mark to \0, and return a pointer to the mark (the strtok function will change the string being operated on, so the characters split using the strtok function are generally It is a temporary copy of the content and allows modification).
    The first parameter of the ?strtok function is not NULL, the function will find the first token in str, and the strtok function will save his position in the string.
    ?If the first parameter of the function is NULL, the function will search for the next token starting at the saved position in the same string.
  • Return value: If the delimiting character of the mark does not exist in the target string, a NULL pointer is returned. If found, the mark is set as the end flag \0.

Function display

Debugging animation display will help you understand the function of the function more clearly

The above is the author’s QQ. Everyone is welcome to learn and communicate together, hehe.

strerror


To use this function, you need to include the header file

  • Function parameters: errnum – error number, usually errno
  • Function: Returns a pointer to an error string that describes the error errnum.
  • Function explanation: This function will search the error number internally and return a pointer to the error message string, that is, the error message character is in the header file. If there is an error when the program is running, strerror will capture the error type and Returns a pointer to the error message character.

Function demonstration:

?Since there is no heihei file in the current file, the file cannot be found when opened in read-only mode, so the error message returned is that there is no related file or the related file has been destroyed.
Friends who have forgotten the file-related operations here can review it by moving to the file super message explanation.

String case conversion

strupr
  • The syntax format is as follows:
    strupr (string); (note that the new compiler requires adding _ when using this function)
  • Function: Convert the lowercase characters in the string to uppercase letters, leaving other letters unchanged.

Function implementation:

strlwr

Same format, opposite functions
The demonstration is as follows:

memcpy

  • Function parameters: target string, source string, quantity (unsigned int), void type can adapt to multiple types.
  • Function function: The function function is very similar to strncpy, except that the types that memcpy can operate are more abundant. Starting from the position of the first element of the source string, copy num bytes of data backward to the position of the target string. This The function does not stop when encountering ‘\0’, and if the source string and target string overlap, the copy result is undefined.

For example, four byte characters need to be copied. The source string is located 3 subsections before the target string.

?The original intention is to copy all 1, 2, 3, and 4. However, during the first copy, element 1 has already covered element 4.
This is related to the implementation of the underlying function, and there is no overlapping processing.
Function simulation is implemented as follows:

void* _memcpy(void* dst, const void* src, size_t count)
{<!-- -->
void* ret = dst; //Record the address of the target array
assert(dst);
assert(src);
while (count--)
{<!-- -->
*(char*)dst = *(char*)src;
dst = (char*)dst + 1;
src = (char*)src + 1;
}
return ret;
}

memmove

?If the function memcpy has an overlapping problem, memmove is used to solve this problem. If there is overlap between the source space and the target space, the memmove function must be used to deal with it.
The function format and function are exactly the same as momcpy.
Function presentation:

Simulation implementation:

void* _memmove(void* dst, const void* src, size_t count)
{<!-- -->
void* ret = dst;
if (dst <= src || (char*)dst >= ((char*)src + count))
{<!-- -->
while (count--)
{<!-- -->
*(char*)dst = *(char*)src;
dst = (char*)dst + 1;
src = (char*)src + 1;
}
}
else//Copy from back to front
{<!-- -->
dst = (char*)dst + count - 1;
src = (char*)src + count - 1;
while (count--)
{<!-- -->
*(char*)dst = *(char*)src;
dst = (char*)dst - 1;
src = (char*)src - 1;
}
}
}

memcmp

  • Compared with two arrays (can be character arrays or other types), num bytes, void* can adapt to multiple types.
  • The function function is the same as strcmp, but it can be adapted to more types.

Function presentation:

The integer is four bytes, and the first two elements of the two arrays are equal, so the return value is 0.

Change to 12 bytes, and the return value becomes -1.