当前位置:网站首页>String copy function
String copy function
2022-06-11 21:23:00 【Hua Weiyun】
1. strcpy
- char * strcpy(char * restrict dst,const char * restrict src);
- hold src Copy the string of to dst
- restrict indicate src and dst No overlap (C99)
- return dst
- In order to chain the code
- To make functions strcpy Can directly participate in operation , So the function strcpy The return value of char * Of dst
2. Copy a string
- char * dst =(char*)malloc(strlen(src)+1)
- strcpy(dst,src);
- I don't know how much memory this thing needs , So you need to dynamically plan a memory
- This length strlen(src) Is the length of the stored content , Excluding ending “\0”, So we need to +1
- The return value type is (void * ), Convert to (char * )
- The mistake that beginners often make is to forget +1.
3. Write one yourself strcpy Functions with the same function
#include <stdio.h>#include <string.h>char *mycpy(char *dst, const char *src) {// int idx = 0;// while(src[]!='\0') {// dst[idx]=src[idx];// idx++;// }// dst[idx]='\0'; char *ret = dst; while (*dst++ = *src++); *dst = '\0'; return ret;}int main(int argc, char const *argv[]) { char s1[] = "abc"; char s2[] = "abc"; printf("%c", s1); printf("%c", s2); printf("%d", strcpy(s1, s2)); return 0;}4. Character found in string
- char * strchr(const char * s,int c);
- Look on the left at s In the string c First occurrence
- char * strrchr(const char * s,int c);
- Find it on the right s In the string c First occurrence
- return NULL Indicates that no
- Return non NULL It means that we have found , The pointer points to the character to be found
- problem : How to find character number 2 The location of the second occurrence
#include <stdio.h>#include <string.h>int main(int argc, char const *argv[]) { char s[] = "hello"; char *p = strchr(s, 'l'); p = strchr(p + 1, 'l'); printf("%s\n", p); return 0;}This code is written as , stay s Find... In this string ‘l’, Assign this pointer to p, That is to say, it can pass through p Access found l The location of . therefore p The first output is ‘llo’, If we want to find the second l, You can go to p In this string , Start strchr, however p The first letter inside is also l, So we can make p+1, So we can find the second one l 了 .\
5. Find this character , Copy the character and its subsequent characters into another variable .
#include <stdio.h>#include <string.h>#include <stdlib.h>int main(int argc, char const *argv[]) { char s[] = "hello"; char *p = strchr(s, 'l'); char *t = (char *)malloc(strlen((p)+ 1); strcpy(t, p); printf("%s\n", p); printf("%s\n", t); free(t); return 0;}This program says , Let's define a t, Then dynamically plan his size , Because of this t It's for copying p Of string , So we need to p String length of ,( Why? strlen You need to +1 Well ?) And then use string.h The string assignment function in p The content of is copied in t in . Finally, don't forget to release t.
边栏推荐
- JVM对象分配策略TLAB
- Object creation process of JVM
- Goland中在文件模板中为go文件添加个人声明
- JS monitor scrolling touch bottom load more_ Browser scrolls to the bottom to load more
- Cuckoo Hash
- [Part 14] source code analysis and application details of completionservice class [key]
- 成长的12条黄金法则
- Cs144 lab0 lab1 record
- JVM之堆区
- Three waves of changes in cloud computing
猜你喜欢

In idea, run the yarn command to show that the file cannot be loaded because running scripts is disabled on this system

Live broadcast with practice | 30 minutes to build WordPress website with Alibaba cloud container service and container network file system

A collection of commonly used open source data sets for face recognition

LeetCode-322-零钱兑换

Use float to create a page header, footer, left content, and main content.

第二部分 数据链路层

How to Load Data from CSV (Data Preparation Part)

Three waves of changes in cloud computing

解决 img 5px 间距的问题

LabVIEW控制Arduino实现超声波测距(进阶篇—5)
随机推荐
可综合RTL代码设计方法和注意事项
【C语言进阶】整型在内存中的存储
如何使用 SAP Kyma 控制台手动发送 SAP Commerce Cloud Mock 应用暴露的事件
Codeforces Round #740 Div. 2 解题报告
Serval and Rooted Tree(CF1153D)-DP
Educational Codeforces Round 114 (Rated for Div. 2) D
Syntax of SQL
【博弈论-绪论】
[Game Theory - introduction]
JUnit tests multithreaded code, and the sub thread does not run
The gateway starts other microservices first. When the gateway is started, the gateway cannot be started and there is no exception log; Start the gateway first, and all services can be started normall
select _ Lazy loading
JMeter load test finds the maximum number of concurrent users (including step analysis)
Test plans and test cases
A collection of commonly used open source data sets for face recognition
BZOJ3189 : [Coci2011] Slika
【C語言進階】整型在內存中的存儲
IDEA中,运行yarn命令,显示无法加载文件,因为在此系统上禁用运行脚本
apache 本地多端口配置
Common file functions