Table of contents
1. Find the length of the string
1,strlen
2. String functions with unlimited length
1,strcpy
2, broken
3,strcmp
3. Introduction to string functions with limited length
1,strncpy
2,strncat
3,strncmp
4. String search
1,strstr
2, strtok
5. Error message reporting
1, strerror
6. Character operation
7. Memory operation function
1,memcpy
2,memmove
3,memset
4,memcmp
Eight, summary
1. Find the length of the string
1,strlen
size_t strlen ( const char * str );
The string has '\0' as the end mark, and the strlen function returns the number of characters that appear before '\0' in the string (excluding '\0' )
The string pointed to by the parameter must end with '\0'
Note that the return value of the function is size_t , which is unsigned (error-prone)
Note :
#include <stdio.h>
int main()
{
const char*str1 = "abcdef";
const char*str2 = "bbb";
if (strlen(str2) - strlen(str1) > 0)
{
printf("str2>str1\n");
}
else
{
printf("srt1>str2\n");
}
return 0;
}
The result of this operation is str1>str2
Because the return value of the strlen function is of size_t type and is an unsigned integer , the return value is a number greater than or equal to 0.
The simulation implementation of strlen function is as follows:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
size_t my_strlen(const char* str)
{
int num = 0;
while (*str++ != '\0')
{
num++;
}
return num;
}
int main()
{
char arr[] = "abcde";
int ret=my_strlen(arr);
printf("%d", ret);
return 0;
}
2. String functions with unlimited length
1,strcpy
char * strcat ( char * destination, const char * source );
Source string must end with '\0'
Will copy '\0' in the source string to the target space
The target space must be large enough to accommodate the source string
The target space must be variable
The simulation implementation of strcpy function is as follows:
#include<stdio.h>
char* my_strcpy(char* arr, const char* arr1)
{
char* p = arr;
while (*arr++ = *arr1++);
return p;
}
int main()
{
char arr[10] = "abcde";
char arr1[] = "ghjkl";
char* ret = my_strcpy(arr, arr1);
printf("%s", arr);
return 0;
}
2, broken
char * strcat ( char * destination, const char * source );
Source string must end with '\0'
The target space must be large enough to accommodate the contents of the source string.
The target space must be modifiable
How about appending the string to itself? -----Appending a string to itself will crash, because '\0' will be overwritten, so the program cannot end, and it will be an infinite loop!
3,strcmp
int strcmp ( const char * str1, const char * str2 );
standard regulation:
If the first string is greater than the second string, a number greater than 0 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 0 is returned.
3. Introduction to string functions with limited length
1,strncpy
char * strncpy ( char * destination, const char * source, size_t num );
Copy num characters from source string to destination space
If the length of the source string is less than num, after copying the source string, append 0 to the end of the target until num
2,strncat
char * strncat ( char * destination, const char * source, size_t num );
dest must have enough space to accommodate the string to be copied
strncat will overwrite the last ' \0 ' of the dest string, and then append ' \0 ' after the character addition is completed;
3,strncmp
int strncmp ( const char * str1, const char * str2, size_t num );
Compare until another character is different or a string ends or num characters are all compared.
4. String search
1,strstr
char * strstr ( const char *str1, const char * str2);
The strstr function searches whether the string str1 contains the string str2. If it exists, it returns the address of the first occurrence of str2 in str1; otherwise, it returns NULL.
Simulation implementation of strstr function:
#include<stdio.h>
#include<assert.h>
char* my_strstr(const char* dest, const char* src)
{
assert(dest&&src);
char* s1 = dest;
while (s1)
{
char* s2 = s1;
char* s3 = src;
while ((s2&&s3) && (*s2 == *s3))
{
s2++;
s3++;
}
if (*s3 == '\0')
return s1;
if (*s2 == '\0')
return NULL;
s1++;
}
}
int main()
{
char arr[] = "abbbcdef";
char brr[] = "bbc";
char* ret = my_strstr(arr, brr);
printf("%s", ret);
return 0;
}
2, strtok
char * strstr ( const char *str1, const char * str2);
The sep parameter is a string that defines the set of characters used as separators.
The first parameter specifies a string containing 0 or more tokens separated by one or more delimiters in the sep string.
The strtok function finds the next token in str, ends it with \0 , and returns a pointer to this token . ( Note: The strtok function will change the manipulated string, so the string split using the strtok function is generally a temporary copy of the content and can be modified. )
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 its position in the string
The first parameter of the strtok function is NULL . The function will start at the saved position in the same string and find the next mark.
If no more tokens exist in the string, a NULL pointer is returned
strtok code example:
#include<stdio.h>
int main()
{
char arr[] = "[email protected]";
char arr1[] = ".@";
char* ret = 0;
for (ret = strtok(arr, arr1); ret != 0;ret=strtok(0,arr1))
{
printf("%s\n", ret);
}
return 0;
}
5. Error message reporting
1, strerror
char * strerror ( int errnum );
When a C language library function fails to execute, there will be an error code (0, 1, 2, 3, 4, 5, 6, 7, 8, 9...)
Return error code and corresponding error message
errno is a global error code variable
When an error occurs during the execution of a C language library function, the corresponding error code will be copied to errno.
strerror code demonstration:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<errno.h>
int main()
{
printf("%s\n", strerror(0));
printf("%s\n", strerror(1));
printf("%s\n", strerror(2));
printf("%s\n", strerror(3));
return 0;
6. Character operation
Character classification function:
function | Returns true if its parameters meet the following conditions |
iscntrl | any control character |
isspace | White space characters: space ' ', form feed '\f', line feed '\n', carriage return '\r', tab '\t' or vertical tab '\v' |
even | Decimal numbers 0~9 |
self digit | Hexadecimal numbers, including all decimal numbers, lowercase letters a~f, uppercase letters A~F |
islower | Lowercase letters a~z |
isupper | Capital letters A~Z |
isalpha | Letters a~z or A~Z |
the ice hall | Letters or numbers, a~z, A~Z, 0~9 |
ispunct | Punctuation marks, any graphic characters that are not numbers or letters (printable) |
i's'isgraph | any graphic character |
sprint | Then printable characters, including graphic characters and whitespace characters |
Character conversion:
int tolower ( int c );
int toupper ( int c );
7. Memory operation function
1,memcpy
void * memcpy ( void * destination, const void * source, size_t num );
The function memcpy copies num bytes of data starting from the source location to the destination memory location.
This function will not stop when it encounters '\0'
If there is any overlap between source and destination, the results of copying are undefined.
Memcpy function simulation implementation:
void* my_memcpy(void* dest, const void* src, size_t num)
{
while (num--)
{
*(char*)dest = *(char*)src;
(char*)dest += 1;
(char*)src += 1;
}
}
int main()
{
char arr[] = "abcde";
char arr1[] = "gjk";
my_memcpy(arr, arr1, sizeof(arr1[0]) * 2);
printf("%s", arr);
}
2,memmove
void * memmove ( void * destination, const void * source, size_t num )
The difference with memcpy is that the source memory block and target memory block processed by the memmove function can overlap.
If the source space and target space overlap, you have to use the memmove function to handle it.
Memmove function simulation implementation:
void* my_memmove(void* dest, const void* src, size_t num)
{
if (dest <= src)
{
while (num--)
{
*(char*)dest = *(char*)src;
(char*)dest += 1;
(char*)src += 1;
}
}
else
{
while (num--)
{
*((char*)dest+num) = *((char*)src+num);
}
}
}
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8 };
my_memmove(arr + 3, arr + 2, sizeof(arr[0]) * 3);
int i = 0;
for (i = 0; i < 8; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
3,memset
void* memset ( void* dest , int c , size_t num );
memset is an initialization function, its function is to set everything in a certain piece of memory to a specified value.
memset code example:
#include<stdio.h>
int main()
{
char arr[] = "abcde";
memset(arr + 1, '#', sizeof(arr[0]) * 3);
printf("%s", arr);
return 0;
}
4,memcmp
int memcmp ( const void * ptr1, const void * ptr2, size_t num );
Compare num bytes starting from the ptr1 and ptr2 pointers
The return value is as follows:
memcmp code example:
#include<stdio.h>
int main()
{
char arr[] = "abcdef";
char brr[] = "abcghj";
int ret = memcmp(arr, brr, sizeof(arr[0]) * 3);
printf("%d\n", ret);
ret=memcmp(arr, brr, sizeof(arr[0]) * 4);
printf("%d", ret);
return 0;
}
Eight, summary
This article is carefully crafted after consulting countless materials. It is highly readable and has a very good effect on both beginners and reviewers!
It is recommended to collect and ponder over and over again!