Table of contents
Functions of unlimited length:
7. strncmp (string comparison)
8. strstr (find string substring)
10. strerror (return error message)
Character manipulation functions:
1. tolower (lowercase -> uppercase)
2. toupper (uppercase -> lowercase)
Find the length of a string:
1. strlen (string length)
size_t strlen ( const char * str );str: C string.Return value: unsigned int.
#include <stdio.h>
#include <string.h>
int main()
{
if ((int)strlen("abc") - (int)strlen("abcdef") > 0)
{
printf("大于\n");
}
else
{
printf("小于等于\n");
}
return 0;
}
Functions of unlimited length:
2. strcpy (string copy)
char* strcpy(char * destination, const char * source );destinationob: Pointer to the destination array where the contents are to be copied .source: The C string to copy.
#include <stdio.h>
#include <string.h>
int main()
{
//char arr1[3] = "";
//char arr2[] = "hello bit";
char* arr1 = "xxxxxxxxxx";
char arr2[6] = { 'a', 'b', 'c', 'd', 'e' , '\0'};
strcpy(arr1, arr2);
printf("%s\n", arr1);
return 0;
}
3. strcat (string append)
char * strcat ( char * destination, const char * source );destination: A pointer to a destination array that should contain C strings and be large enough to contain the resulting strings of the concatenation.source: The C string to append. This should not overlap the target .
#include <stdio.h>
#include <string.h>
int main()
{
char arr1[20] = "hello ";
char arr2[] = "world";
strcat(arr1, arr2);
printf("%s\n", arr1);
return 0;
}
4. strcmp (string comparison)
int strcmp ( const char * str1, const char * str2 );str1: The C1 string to compare.str2 : The C2 string to compare.
#include <stdio.h>
#include <string.h>
int main()
{
int ret = strcmp("bbq", "bcq");
if (ret>0)
printf(">\n");
printf("%d\n", ret);
return 0;
}
Length-restricted functions:
5. strncpy (string copy)
char * strncpy ( char * destination, const char * source, size_t num );destination: Pointer to the destination array where the contents are to be copied.source: The C string to copy.num : the maximum number of characters to copy from the source ;size_t is an unsigned integer type.
#include <stdio.h>
#include <string.h>
int main()
{
char arr1[20] = "abcdef";
char arr2[] = "xxx";
strncpy(arr1, arr2, 5);
return 0;
}
6. strncat (string append)
char * strncat ( char * destination, const char * source, size_t num );destination: A pointer to a destination array that should contain a C string and be large enough to contain the resulting string of concatenation, including additional null characters.source: The C string to append.
#include <stdio.h>
#include <string.h>
int main()
{
char arr1[20] = "abcdef\0yyyyyyyy";
char arr2[] = "xxxxxxxxx";
strncat(arr1, arr2, 3);
return 0;
}
7. strncmp (string comparison)
int strncmp ( const char * str1, const char * str2, size_t num );str1: The C1 string to compare.str2: The C2 string to compare.
#include <stdio.h>
#include <string.h>
int main()
{
char arr1[] = "abcqwertyuiop";
char arr2[] = "abcdef";
printf("%d\n", strncmp(arr1, arr2, 4));
return 0;
}
String lookup:
8. strstr (find string substring)
char * strstr ( const char *str1, const char * str2);str1: C string to scan.str2: C string containing the sequence of characters to match.
#include <stdio.h>
#include <string.h>
int main()
{
char arr1[] = "abbbcdef";
char arr2[] = "bbc";
char* ret = strstr(arr1, arr2);
if (ret != NULL)
printf("%s\n", ret);
else
printf("找不到\n");
return 0;
}
9. strtok (string split)
char * strtok ( char * str , const char * sep );str: C string to truncate. Note that this string is modified by breaking it into smaller strings (tokens). Alternatively, a null pointer can be specified, in which case the function will continue scanning the location of previous successful function calls.sep : A C string containing the separator character. These may vary from call to call.
#include <stdio.h>
#include <string.h>
int main()
{
char arr[] = "[email protected]@666#777";
char copy[30];
strcpy(copy, arr);
char sep[] = "@.#";
char* ret = NULL;
for (ret = strtok(copy, sep); ret != NULL; ret=strtok(NULL, sep))
{
printf("%s\n", ret);
}
return 0;
}
Error message report:
10. strerror (return error message)
char * strerror ( int errnum );errnum : error number.When the library function is executed, if a misplacement occurs, an error code will be stored in the variable errno, where errno is a global variable provided by the C language.
#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;
}
Character manipulation functions:
function |
Returns true if its argument meets the following conditions
|
---|---|
iscntrl
|
any control characters
|
isspace
|
Whitespace characters: space
' '
, form feed
'\f'
, line feed
'\n'
, carriage return
'\r'
, tab
'\t'
or vertical tab
'\v'
|
even
|
Decimal number
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
|
Letter
a~z
or
A~Z
|
the ice hall
|
Letter or number,
a~z,A~Z,0~9
|
ispunct
|
Punctuation, any graphic character that is not a number or letter (printable)
|
isgraph
|
any graphic character
|
sprint
|
Any printable character, including graphic characters and whitespace
|
Character conversion:
1.towlower (lowercase -> uppercase)
int tolower ( int c );
c: The character to convert, convert to int or EOF.
#include <stdio.h>
#include <ctype.h>
int main()
{
printf("%c\n", tolower('A'));
printf("%c\n", tolower('s'));
return 0;
}
2. toupper (uppercase -> lowercase)
int toupper ( int c );
c: The character to convert, convert to int or EOF.
#include <stdio.h>
#include <ctype.h>
int main()
{
char arr[20] = { 0 };
gets(arr);//遇到空格继续读
char* p = arr;
while (*p)
{
if (isupper(*p))// *p>='A' && *p<='Z'
{
*p = tolower(*p);//*p = *p+32;
}
p++;
}
printf("%s\n", arr);
return 0;
}
Memory manipulation function:
1. memcpy (memory copy)
void * memcpy ( void * destination, const void * source, size_t num );destination: Pointer to the destination array in which to copy the content, typecast to a pointer of type void*.source: A pointer to the data source to be copied, converted to a pointer of type const void*.
#include <stdio.h>
#include <string.h>
int main()
{
int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
int arr2[20] = { 0 };
//将arr1中的内容,拷贝到arr2中
memcpy(arr2, arr1, 40);
int* int*
int i = 0;
for (i = 0; i < 20; i++)
{
printf("%d ", arr2[i]);
}
return 0;
}
2. mommove (memory copy)
void * memmove ( void * destination, const void * source, size_t num );destination: Pointer to the destination array in which to copy the content, typecast to a pointer of type void*.source: A pointer to the data source to be copied, converted to a pointer of type const void*.
#include <stdio.h>
#include <string.h>
int main()
{
int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
// 1 2 1 2 3 4 5 8 9 10
memmove(arr1, arr1+2, 20);
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", arr1[i]);
}
return 0;
}
3. memcmp (memory comparison)
int memcmp ( const void * ptr1, const void * ptr2, size_t num );ptr1: Pointer to the memory block.ptr2: Pointer to the memory block.num : The number of bytes to compare.
#include <stdio.h>
#include <string.h>
int main()
{
int arr1[] = { 1,2,1,4,5,6 };
int arr2[] = { 1,2,257 };
int ret = memcmp(arr1, arr2, 10);
printf("%d\n", ret);
return 0;
}
4. memset (memory settings )
void * memset ( void * ptr1, int value, size_t num );
ptr1: Pointer to the memory block to be filled.
value: The value to set. The value is passed as an int, but the function fills the memory block with an unsigned char conversion of this value .
num : The number of bytes to set to the value.
size_t is an unsigned integer type.
#include <stdio.h>
#include <string.h>
int main()
{
char arr[] = "hello bit";
memset(arr+1,'x',4);//以字节为单位设置的
printf("%s\n", arr);
return 0;
}
The above is the analysis of personal learning insights and learning, welcome everyone to discuss in the comment area!
Thanks for the one-click three-connection guys! Thanks for the one-click three-connection guys! Thanks for the one-click three-connection guys!