【C语言】关于字符串函数的使用及模拟实现(2)

一、字符串的复制

1.1 库函数strcpy的使用

在函数strcpy中,函数的返回类型为char* ,参数部分首先是指向目标地址的指针,其次是指向源地址的指针(由于源地址中内容不受影响,则可以使用const修饰),函数所需的头文件为string.h

6419a0dc136b467bad5b4e516e5f3068.png

1dbbff94c25e4708bdc079544a8f0bdd.png

 

1.2 库函数strncpy的使用

strncpy函数在strcpy函数基础上加入了一个参数,表示复制到目标地址去的个数

e894fff37cf743d0976a1318cae70a29.png

98288e4fa23c4e6aab34ea9e8ad8dd67.png

 

1.3 模拟实现strcpy及strncpy

模拟实现库函数strcpy

#include<stdio.h>
char* my_strcpy(char* b,const char* a){
	char* tmp = b;
	while(*a){
		*b = *a;
		a++;
		b++;
	}
	return tmp;
}
int main(){
	char a[20] = "abcd";
	char b[20];
	char c[20];
	printf("b[] = %s\n",my_strcpy(b,a));
	printf("c[] = %s\n",my_strcpy(c,"dsda"));
	return 0;
}

模拟实现库函数strncpy

#include<stdio.h>
char* my_strncpy(char* b,const char* a,int c){
	int count = 0;
	char* tmp = b;
	while(count < c){           //for(int count=0;count<c;a++,b++,count++){
		*b = *a;                //    *b = *a;
		a++;                    //	}
		b++;
		count++;
	}
	return tmp;
}
int main(){
	char a[20] = "abcd";
	char b[20];
	char c[20];
	printf("b[] = %s\n",my_strncpy(b,a,3));
	printf("c[] = %s\n",my_strncpy(c,"sdas",2));
	return 0;
}

二、字符串的比较

2.1 库函数strcmp的使用

函数的返回类型为int,所需的头文件为string.h

218efc6f558f4d1584df85099ca6ac09.png

 比较规则:

13713ba9602846269e29710266e1e0fd.png

 437532010ff649ee8d08bf27df85c1c5.png

2.2 库函数strncmp的使用

7debd498ab6b45ca8fc990ead443194b.png

 6baeeeef38e648a4951a8b0aaffdfbc8.png

2.3 模拟实现strcmp及strncmp

模拟实现库函数strcmp

#include<stdio.h>
int my_strcmp(const char* a,const char* b){
	while(*a || *b){
		if(*a > *b){
			return 1;
		}else if(*a < *b){
			return -1;
		}
		a++;
		b++;
	}
	return 0;
}
int main(){
	char a[20] = "abcd";
	char b[20] = "abdd";
	int tmp = my_strcmp(a,b);
	printf("%d\n",tmp);
	tmp = my_strcmp(b,a);
	printf("%d\n",tmp);
	tmp = my_strcmp(a,a);
	printf("%d\n",tmp);
	return 0;
}

模拟实现库函数strncmp

#include<stdio.h>
int my_strncmp(const char* a,const char* b,int c){
	int count = 0;
	while((*a || *b) && count < c){
		if(*a > *b){
			return 1;
		}else if(*a < *b){
			return -1;
		}
		a++;
		b++;
		count++;
	}
	return 0;
}
int main(){
	char a[20] = "abcd";
	char b[20] = "abdd";
	int tmp = my_strncmp(a,b,3);
	printf("%d\n",tmp);
	tmp = my_strncmp(a,b,2);
	printf("%d\n",tmp);
	return 0;
}

三、字符串的切割

3.1 库函数strtok的使用

函数返回类型为char* ,首个参数为被切割的字符串,其次是切割字符串的分割符。

strtok函数会改变被操作的字符串,所以在使用strtok函数时一般要先临时拷贝一份

4adfb2396f024f0780c83f3be5a85900.png

#include<stdio.h>
#include<string.h>
int main(){
	char a[20] = "abcd#abc^ab#a";
	char* p = NULL;
	p = strtok(a,"#^");    //首次调用时,函数找到第一个标记并记录在字符串中的位置,并将其改为\0
	while(p != NULL){
		printf("%s\n",p);
		p = strtok(NULL,"#^");   //函数在同一字符串中的保存位置开始查找
	}
	return 0;
}
#include<stdio.h>
#include<string.h>
int main(){
	char a[20] = "abcd#abc^ab#a";
	char* p = NULL;
	for(p = strtok(a,"#^");p != NULL;p = strtok(NULL,"#^")){
		printf("%s\n",p);
	}
	return 0;
}

 

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值