当前位置:网站首页>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.

原网站

版权声明
本文为[Hua Weiyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206112120227313.html