1. Character function
They are roughly divided into character classification functions and character conversion functions:
Character classification function:
iscntrl //任何控制字符
isspace //空白字符:空格‘ ’,换页‘\f’,换行'\n',回车‘\r’,制表符'\t'或者垂直制表符'\v'
isdigit //十进制数字 0~9
isxdigit //十六进制数字,包括所有十进制数字,小写字母a~f,大写字母A~F
islower //小写字母a~z
isupper //大写字母A~Z
isalpha //字母a~z或A~Z
isalnum //字母或者数字,a~z,A~Z,0~9
ispunct //标点符号,任何不属于数字或者字母的图形字符(可打印)
isgraph //任何图形字符
isprint //任何可打印字符,包括图形字符和空白字符
for example
As long as it matches the character, a non-zero number will be returned. If it does not match, it will return 0.
#include <ctype.h>
#include <stdio.h>
int main()
{
printf("%d\n", isupper('B'));
printf("%d\n", isdigit('3'));
printf("%d\n", isupper('b'));
return 0;
}
Character conversion function:
int tolower ( int c );
int toupper ( int c );
To convert uppercase characters to lowercase characters, use tolower, and to convert lowercase characters to uppercase characters, use toupper.
for example
#include <ctype.h>
#include <stdio.h>
int main()
{
printf("%c\n", tolower('B'));
printf("%c\n", tolower('b'));
printf("%c\n", tolower('2'));
printf("%c\n", toupper('B'));
printf("%c\n", toupper('b'));
printf("%c\n", toupper('2'));
return 0;
}
2. String function
strlensize_t strlen ( const char * str );The string has been '\0' as the end mark, strlen< The /span> . ) in the string ( Does not contain'\0' '\0' function returns the number of characters that appear beforeThe 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 )
#include <stdio.h>
#include <string.h>
int main()
{
size_t sz = my_strlen("abc");
printf("%u\n", sz);
return 0;
}
strcpychar* strcpy ( char * destination , char * source);The 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.
#include <stdio.h>
#include <string.h>
int main()
{
char arr1[20] = "XXXXXXXXXXXXXXXXXXXX";
char arr2[] = "abdef";
strcpy(&arr1[1], &arr2[1]);
printf("%s\n", arr1);
return 0;
}
screwed up
char * strcat ( char * destination, const char * source );
The 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.You can’t add it to yourself
#include <stdio.h>
#include <string.h>
int main()
{
char arr1[20] = "hello ";
char arr2[] = "world";
strcat(arr1, arr2);
printf("%s", arr1);
return 0;
}
strcmp
int strcmp ( const char * str1, const char * str2 );
The first string is greater than the second string, then return a number greater than 0The first string is equal to the second string, then return 0The first string is less than the second string, then return a number less than 0
#include <stdio.h>
#include <string.h>
int main()
{
int sz = strcmp("abc\0a", "abc");
printf("%d\n", sz);
return 0;
}
strncpy
char * strncpy ( char * destination, const char * source, size_t num );
Copy num characters from the source string to the target space.If the length of the source string is less than num , after copying the source string, append < to the end of the target a i=3>0 , untilnum.
#include <stdio.h>
#include <string.h>
int main()
{
char arr1[20] = "abdsefagafs";
char arr2[] = "xxxxxxx";
strncpy(arr1 + 2, arr2, 3);
printf("%s\n", arr1);
return 0;
}
strncat
char * strncat ( char * destination, const char * source, size_t num );
The target space must be large enough to accommodate the contents of the source string.The target space must be modifiable.You can add it yourself.
#include <stdio.h>
#include <string.h>
int main()
{
char arr1[20] = "abdafada\0aaaaaaa";
char arr2[] = "x";
strncat(arr1, arr1, 5);
printf("%s\n", arr1);
return 0;
}
strncmp
int strncmp ( const char * str1, const char * str2, size_t num );
Compare until another character is different or a string ends or all num characters are compared.The return value is similar to strcmp.
Example
#include <stdio.h>
#include <string.h>
int main()
{
char arr1[] = "abcedf";
char arr2[] = "abc";
printf("%d\n", strncmp(arr1, arr2, 3));
return 0;
}
strstr
char * strstr ( const char * str1 , const char * str2 );Used to find whether a substring exists in a string.If it exists, the corresponding first character address found for the first time is returned. If it does not exist, NULL is returned.
Example
#include <stdio.h>
#include <string.h>
int main()
{
char arr1[] = "abcdefabcdef";
char arr2[] = "bc";
char* ret = strstr(arr1, arr2);
if (ret != NULL)
printf("%s\n", ret);
else
printf("找不到\n");
return 0;
}
strtok
char * strtok ( char * str, const char * sep );
sep The parameter is a string that defines the set of characters used as separatorsThe first parameter specifies a string, which contains 0 or more by sep Tag separated by one or more separators in a string.The strtok function finds the next token in str and ends it with \0, Returns a pointer to this tag (Note:The strtok function will change the string being operated on, so when using the strtok function to split Strings are generally the contents of temporary copies and can be modified. )strtok The first parameter of the function is not NULL , the function will find The first token in str , strtok function will save its position in the string .strtok The first parameter of the function is NULL , the function will be in the same string Starting from the saved position, search for the next mark.If there are no more tokens in the string, a NULL pointer is returned.
Example
#include <stdio.h>
#include <stdio.h>
int main()
{
char arr[] = "agca?dfawef?dsgd@afdsa";
char copy[100];
char a[] = ".?";
strcpy(copy, arr);
char* ret = NULL;
for (ret = strtok(copy, a); ret != NULL; ret = strtok(NULL, a))
{
printf("%s\n", ret);
}
return 0;
}
strerror
char * strerror ( int errnum );
Returns the error code and corresponding error message.
Example
#include <stdio.h>
#include <string.h>
int main()
{
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d:%s\n", i, strerror(i));
}
return 0;
}
3. Memory function
memcpyvoid * memcpy ( void * destination , const void * source , size_t num );function memcpy starts from the position of source backwards Copy num bytes of data to the memory location of destination .This function will not stop when encountering '\0' .If there is any overlap between source and destination , The results of copying are undefined.
Example
#include <stdio.h>
#include <string.h>
int main()
{
int arr1[] = { 1,2,4,5,6,7,8,9,10 };
int arr2[20] = { 0 };
memcpy(arr2, arr1,21);
int i = 0;
for (i = 0; i < 20; i++)
{
printf("%d ", arr2[i]);
}
return 0;
}
memmove
void * memmove ( void * destination, const void * source, size_t num );
The difference between and memcpy is that the memmove function handles Source memory blocks and target memory blocks can overlap.If the source space and target space overlap, you have to use the memmove function.
#include <stdio.h>
#include <string.h>
int main()
{
int arr1[] = { 1,2,4,5,6,7,8,9,10 };
int arr2[20] = { 0 };
memmove(arr1, arr1, 20);
int i = 0;
for (i = 0; i < 9; i++)
{
printf("%d ", arr1[i]);
}
return 0;
}
memcmp
int memcmp ( const void * ptr1 , const void * ptr2 , size_t num );comparison ptr1 和 ptr2 finger opening< /span> character number numThe return value is as follows:
Example
#include <stdio.h>
#include <string.h>
int main()
{
int arr1[] = { 1,2,3,1};
int arr2[] = { 1,2,3,257};
int ret = memcmp(arr1, arr2, 13);
printf("%d", ret);
return 0;
}