1. strlen (for length)
size_t strlen ( const char * str )
The return value type of the function is size_t, which is an unsigned number, and the return value of strlen is the number of characters before '\0' in the string.
Guess the output of this program
if (strlen("abc") - strlen("abcdef"))
{
printf(">");
}
else
{
printf("<");
}
Yes, yes>
Because the return value of strlen is unsigned, -3 becomes a large positive integer.
2. strcpy (copy)
char* strcpy (char * destination, const char * source)
When used, the source string must end with \0, and '\0' will also be copied
strcpy: "I don't care if you can put it or not, I will copy it."
The target address must be large enough and modifiable, otherwise, although it can be put in, the program will crash
Simulation implementation of strcpy
char* my_strcpy(char* dest,const char* source)
{
assert(dest && source);
char* ret = dest;
while (*dest++ = *source++);
return ret;
}
3. strcat (append)
char * strcat ( char * destination , const char * souce)
The source string must have '\0', and the target string must also have '\0'
After appending, the '\0' of the target space is gone, and the '\0' of the source string is added
The destination address must be large and modifiable
Simulation implementation of strcat
char* my_strcat(char* dest,char* source)
{
assert(dest && source);
char* ret = dest;
while (*dest)
{
dest++;
}
while (*dest++ = *source++);
return ret;
}
Four.strcmp
int strcmp(const char* string1, const char * string2)
The strcmp function compares the length of the function that is not a string`
Instead, compare the size of the characters at the corresponding positions in the string. If they are the same, compare the next pair of characters to know that they are different, or encounter '\0'.
Returns 0 if it is the same, and returns a number greater than zero if it is greater. Returns a number less than zero if less than
Simulation implementation of strcmp
int my_strcmp(const char* str, const char* qtr)
{
assert(str && qtr);
while (*str == *qtr)
{
if (*str == '\0')
return 0;
str++;
qtr++;
}
if (*str > *qtr)
return 1;
else
return -1;
}
Five.strncpy
char * strncpy ( char * dest, const char * source , size_t count)
The function is to copy the first count characters of the source string to the target string
char a[] = "abcd";
char b[] = "qwer";
strncpy(a,b,1);
For example, the modified string above is qbcd
Simulation implementation of strncpy
#define _CRT_SECURE_NO_WARNINGS 1
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
void my_strncat(char*dest,char*src,size_t n)
{
int t = strlen(dest);
int f = strlen(src);
for (int i = 0; i < n; i++)
{
*(dest + i) = *src;
src++;
}
}
int main()
{
int n;
scanf("%d", &n);
char src[50], dest[50];
strcpy(dest, "NICE");
strcpy(src, "SHOOT");
my_strncat(dest,src,n);
printf("%s", dest);
}
Six.strncat
char * strncat ( char * dest , const char * source ,size_t count)
The function is to append the first count characters of the source string to dest,
and will add '\0' at the end
char arr1[]="abcdef\0xx";
char arr2[]="qwer";
strncat(arr1,arr2,2);
Modified to abcdefqw
Simulation implementation of strncat
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<assert.h>
void my_strncpy(char* str, char* qtr, int size)
{
assert(str && qtr);
int t = strlen(str);
for (int i = 0; i < size ; i++)
{
*(str + t + i) = *qtr;
qtr++;
}
*(str + t + size) = '\0';
}
int main()
{
char src[50], dest[50];
strcpy(src, "A nice");
strcpy(dest, " BOYYYYYYY");
int n = 0;
scanf("%d", &n);
my_strncpy(src, dest, n);
printf("%s", src);
}
Seven.strncmp
int strncm ( const char * str , const char * qtr , size_t num )
Specifies to compare the first num characters, others are the same as strcmp
Eight.strstr
const char * strstr ( const char * str1, const char * str2 ); char * strstr ( char * str1, const char * str2 );
Used to find substrings, returns the first found address
Nine.strtok
char * strtok ( char * str , const char * sep)
The parameter sep is a string consisting of delimiters
str is a string containing zero or more delimiters in sep.
strtok will find the first delimiter in str, replace it with '\0', and return a pointer to this delimiter
If the first parameter is '\0'', look for another separator backward from the position saved in the previous wave
Returns a NULL pointer if there are no more delimiters in the string
char a[] = "[email protected]";
char sep[] = "@.";
printf("%s", strtok(a, sep));
printf("%s", strtok(NULL, sep));
If the above result is Anice
char a[] = "[email protected]";
const char sep[] = "@.";
char arr[30];
char* str = NULL;
strcpy(arr, a);
for (str = strtok(arr, sep); str != NULL; str = strtok(NULL, sep))
{
printf("%s", str);
}
The above output is Aniceboy
Ten.strerror
char * strerror (int errnum )
errno is a global variable that records the last error code. When the program is just started, errno is set to 0; during the running of the program, any error in any function may modify the value of errno to make it non-zero Value to inform the user that a specific type of error occurred.
Eleven.memcpy
void * memcpy ( void* dest , const void * source ,size_t count)
The role is to copy the first count bytes of the source string to dest
int a1[20] = {1,2,3,4,5,6,7,8};
int a2[5] = {0};
memcpy(a1,a2,20);
After execution, the first five elements of the a1 array become 0
Mock implementation of memcpy:
Since the copy of memcpy is carried out byte by byte
The implementation can be simulated with the following code
void* my_memcpy(void* dest, const void* source, size_t count)
{
assert(source && dest);
void* ret = dest;
while (count--)
{
*(char*)dest = *(char*)source;
source=(char*)source+1;
dest=(char*)dest+1;
}
return ret;
}
Twelve.memmove
void * memmove (void * dest ,const void * source ,size_t count)
Overlapping memory copies can be implemented
Mock implementation:
void* my_memmove(void* dest, const void* source, size_t count)
{
if (dest < source)
{
while(count--)
{
*(char*)dest = *(char*)source;
dest = (char*)dest + 1;
source = (char*)source + 1;
}
}
else
{
while (count--)
{
*((char*)dest + count )= *((char*)source + count);
}
}
}
Thirteen.memcmp
void * memcpy ( void * str , const void * qtr , size_t count)
Compare the first count bytes of str and qtr, note that it is a byte-by-byte comparison
That's interesting
Please see the following code
int a[5] = { 1,2,3,4,5 };
int b[5] = { 1,2,3,4,0x11223305};
memcmp(a,b,16);
Obviously the first sixteen bytes of the array are the same and return 0, but what about the first seventeen
This is because 5 is stored as 05 00 00 00
0x11223305 is 05 33 22 11
Fourteen.memset
void * memset ( void * dest , int c ,size_t count)
The function is to change the first count bytes to the parameter c
int a[]={0,0x22222222};
memset(a,1,8);
For example, the above a array becomes 0x01010101, 0x0101010101