当前位置:网站首页>Character function and string function (2)
Character function and string function (2)
2022-07-25 20:36:00 【Yuan_ o_】
String function with limited length :
strncpy
strnzat
strncmp
1.5 strncpy
char * strncpy ( char * destination, const char * source, size_t num );
- Copies the first num characters of source to destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it.
- 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 , Add... After the target 0, until num individual .
#include <stdio.h>
int main()
{
char arr1[20] = "abcdef";
char arr2[] = "hello";
strncpy(arr1, arr2, 5);
// The transfer parameters are : Objective function , Source function , Number of parameters passed
char arr3[] = "xyz";
strncpy(arr1, arr3, 5);
// When the number of passed parameters is greater than the number of elements in the source function , Append 0
printf("%s", arr1);
return 0;
}
1.6 strncat
char * strncat ( char * destination, const char * source, size_t num );
- Appends the first num characters of source to destination, plus a terminating null-character.
- If the length of the C string in source is less than num, only the content up to the terminating null-character is copied.
#include <stdio.h>
int main()
{
char arr1[20] = "abcdef";
char arr2[] = "hello";
strncat(arr1, arr2, 5);
//strncat The function will automatically add one after the addition '\0'
char arr3[] = "xyz";
strncat(arr1, arr3, 5);
// When the parameters of the source function are less than the number of passed parameters , There is only one in the back '\0'
printf("%s\n", arr1);
return 0;
}
1.7 strncmp
int strncmp ( const char * str1, const char * str2, size_t num );
- Compare to two different characters or the end of a string or num Compare all the characters
The standard stipulates :
- The first string is larger than the second string , Return greater than 0 The number of
- The first string is equal to the second string , Then return to 0
- The first string is less than the second string , Then return less than 0 The number of
#include <stdio.h>
int main()
{
char arr1[20] = "abcdef";
char arr2[] = "abc";
int ret = strncmp(arr1, arr2, 3);
if (ret == 0)
{
printf("==\n");
}
else if (ret > 0)
{
printf(">\n");
}
else
{
printf("<\n");
}
return 0;
}
1.8 strstr( Function to find substring )
const char * strstr ( const char * str1, const char * str2 ); char * strstr ( char * str1, const char * str2 );
- Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.
- The matching process does not include the terminating null-characters, but it stops there.
#include<stdio.h>
int main()
{
char arr1[] = "hello world";
char arr2[] = "world";
char* ret = strstr(arr1, arr2);
if (ret == NULL)
{
printf(" Substring does not exist \n");
}
else
{
printf("%s\n", ret);
// If the source string exists in the destination string , Will return the found address
}
return 0;
}
Simulation Implementation strstr function :
#include <stdio.h>
#include <assert.h>
char* my_strstr(const char* str1, const char* str2)
{
assert(str1 && str2);
const char* s1 = str1;
const char* p = str1;
const char* s2 = str2;
while (*p)
{
s1 = p;
s2 = str2;
while (*s1 != '\0' && *s2 != '\0' && *s1 == *s2)
{
s1++;
s2++;
}
if (*s2 == '\0')
{
return (char*)p;
}
p++;
}
return NULL;
}
int main()
{
char arr1[] = "abbbcedf";
char arr2[] = "bbc";
char* ret = my_strstr(arr1, arr2);
if (ret == NULL)
{
printf(" Substring does not exist \n");
}
else
{
printf("%s\n", ret);
}
return 0;
}
1.9 strtok
char * strtok ( char * str, const char * sep );
- sep The parameter is a string , Defines the set of characters used as separators
- The first parameter specifies a string , It contains 0 One or more by sep A mark separated by one or more separators in a string
- strtok Function found str The next mark in , And use it
\0ending , Returns a pointer to the tag .( notes :strtok Function changes the string being manipulated , So it's using strtok The string cut by the function is usually a temporary copy and can be modified .) - strtok The first argument of the function is not
NULL, Function will find str The first mark in ,strtok Function will hold its position in the string . - strtok The first argument to the function is
NULL. The function will start at the same position in the string that is saved , Find next tag . - If there are no more tags in the string , Then return to
NULLThe pointer .
#include <stdio.h>
#include <string.h>
int main()
{
char arr[] = "https://www.baidu.com";
char cp[30] = {
0 };
const char* sep = ":.";
strcpy(cp, arr);// Make a copy of the data , Handle cp Contents of array
char* ret = strtok(cp, sep);
if (ret != NULL);
printf("%s\n", ret);
ret = strtok(NULL, sep);
if (ret != NULL);
printf("%s\n", ret);
ret = strtok(NULL, sep);
if (ret != NULL);
printf("%s\n", ret);
ret = strtok(NULL, sep);
if (ret != NULL);
printf("%s\n", ret);
return 0;
}
// After simplification and modification :
#include <stdio.h>
#include <string.h>
int main()
{
char arr[] = "https://www.baidu.com";
char cp[30] = {
0 };
const char* sep = ":.";
strcpy(cp, arr);
for (char* ret = strtok(cp, sep); ret != NULL; ret = strtok(NULL, sep))
{
printf("%s\n", ret);
}
return 0;
}
1.10 strerror
char * strerror ( int errnum );
Return the error message corresponding to the error code .
C When the library function of language fails to execute , Will set the error code
#include <stdio.h>
int main()
{
printf("%s\n", strerror(0));
printf("%s\n", strerror(1));
printf("%s\n", strerror(2));
printf("%s\n", strerror(3));
printf("%s\n", strerror(4));
printf("%s\n", strerror(5));
printf("%s\n", strerror(6));
return 0;
}

//errno - C A global error code storage variable of language settings
#include <stdio.h>
#include <errno.h>
int main()
{
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 1;
}
else
{
//
}
return 0;
}
Character classification function :
#include <stdio.h>
#include <ctype.h>
int main()
{
//isspace
int a = isspace(' ');
printf("%d\n", a);
//isdigit
int b = isdigit('x');
printf("%d\n", b);
//tolower
printf("%c\n", tolower('w'));
}

Character conversion :
int tolower ( int c );
int toupper ( int c );
1.11 memcpy
void * memcpy ( void * destination, const void * source, size_t num );
- function memcpy from source The position of the starts to be assigned backwards num Bytes of data to destination Memory location for .
- This function is encountering
'\0'It doesn't stop . - If source and destination There is any overlap , The results of replication are undefined (memcpy Be responsible for copying data in two independent spaces ).
//memcpy
#include <stdio.h>
#include <string.h>
int main()
{
int arr1[] = {
1,2,3,4,5,6,7 };
int arr2[10] = {
0 };
memcpy(arr2, arr1, 28);
float s1[] = {
1.0,2.0,3.0,4.0,5.0 };
float s2[20] = {
0 };
memcpy(s2, s1, 20);
return 0;
}
Simulation Implementation memcpy function :
// Simulation Implementation memcpy function
#include <stdio.h>
#include <assert.h>
void* my_memcpy(void* dest, const void* src, size_t num)
{
assert(dest && src);
void* ret = dest;
while (num--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
return ret;
}
int main()
{
int arr1[] = {
1,2,3,4,5,6,7 };
int arr2[10] = {
0 };
my_memcpy(arr2, arr1, 28);
int i = 0;
for(i = 0; i < 5; i++)
{
printf("%d ", arr2[i]);
}
return 0;
}
1.12 memmove
void * memmove ( void * destination, const void * source, size_t num );
- and memcpy The difference is that memmove The source memory blocks processed by the function can be overlapped .
- If the source space and the target space overlap , You have to use memove Function processing .
Simulation Implementation memmove function :
// Simulation Implementation memmove function
#include <stdio.h>
#include <assert.h>
void* my_memmove(void* dest, const void* src, size_t num)
{
assert(dest && src);
void* ret = dest;
if (dest < src)
{
while (num--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
}
else
{
while (num--)
{
*((char*)dest + num) = *((char*)src + num);
}
}
return ret;
}
int main()
{
int arr1[] = {
1,2,3,4,5,6,7,8,9,10 };
my_memmove(arr1, arr1+2, 20);
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", arr1[i]);
}
return 0;
}
1.13 memcmp
int memcmp ( const void * ptr1, const void * ptr2, size_t num );
- Compare from ptr1 and ptr2 The pointer starts with num Bytes
- The return value is as follows :

Example :
//memcmp
#include <stdio.h>
#include <string.h>
int main()
{
int arr1[] = {
1,2,3,4,5 };
int arr2[] = {
1,3,2 };
int ret = memcmp(arr1, arr2, 12);
printf("%d\n", ret);
return 0;
}
1.14 memset
void * memset ( void * ptr, int value, size_t num );
- take ptr Of the memory block pointed to num Set the number of bytes to the specified value
Example :
//memset
#include <stdio.h>
#include <string.h>
int main()
{
char arr[] = "hello world";
memset(arr, 'x', 5);
memset(arr + 6, 'x', 3);
printf("%s\n", arr);
int arr[10] = {
0 };
// hold arr Initialize to full 1( This method is not feasible )
memset(arr, 1, 40);
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d\n", arr[i]);
}
return 0;
}
边栏推荐
- "Chain" connects infinite possibilities: digital asset chain, wonderful coming soon!
- JS scope and scope chain
- How to choose a microservice registration center?
- Vulnhub | dc: 5 | [actual combat]
- 毕业从事弱电3个月,我为什么会选择转行网络工程师
- Difference Between Accuracy and Precision
- Remote monitoring solution of intelligent electronic boundary stake Nature Reserve
- leetcode-6127:优质数对的数目
- [today in history] July 15: Mozilla foundation was officially established; The first operation of Enigma cipher machine; Nintendo launches FC game console
- 网络RTK无人机上机测试[通俗易懂]
猜你喜欢

leetcode-155:最小栈

ROS_ Rqt toolbox

Vivo official website app full model UI adaptation scheme
![Vulnhub | dc: 5 | [actual combat]](/img/c6/34117bbfb83ebdf9e619f4e4590661.png)
Vulnhub | dc: 5 | [actual combat]
![[today in history] July 2: BitTorrent came out; The commercial system linspire was acquired; Sony deploys Playstation now](/img/7d/7a01c8c6923077d6c201bf1ae02c8c.png)
[today in history] July 2: BitTorrent came out; The commercial system linspire was acquired; Sony deploys Playstation now
![[advanced mathematics] [8] differential equation](/img/83/b6b07540e3cf6d6433e57447d42ee9.png)
[advanced mathematics] [8] differential equation

程序的编译和运行

LeetCode通关:哈希表六连,这个还真有点简单
![[advanced mathematics] [4] indefinite integral](/img/4f/2aae654599fcc0ee85cb1ba46c9afd.png)
[advanced mathematics] [4] indefinite integral

【高等数学】【4】不定积分
随机推荐
Implementation of simple registration and login
Link list of sword finger offer question bank summary (III) (C language version)
Remote monitoring solution of intelligent electronic boundary stake Nature Reserve
Step num problem
Vulnhub | dc: 6 | [actual combat]
Online XML to JSON tool
QML combines qsqltablemodel to dynamically load data MVC "recommended collection"
During the interview, I was asked how to remove the weight of MySQL, and who else wouldn't?
【单细胞高级绘图】07.KEGG富集结果展示
Embedded development: embedded foundation -- threads and tasks
Detailed explanation of document operation
ROS_ Rqt toolbox
Rand1 generates rand9
网络爬虫原理解析「建议收藏」
[advanced drawing of single cell] 07. Display of KEGG enrichment results
Arrow 之 Parquet
Vivo official website app full model UI adaptation scheme
移动web布局方法
The database empties the table data and makes the primary key start from 1
JMeter - interface test