#include <string.h>文件函数详解

#include <string.h>文件函数详解

1.strlen()

在这里插入代码片
#include <stdio.h>
#include <stdlib.h>
//此部分为直接调用string库里的strlen()函数
//int main() {
//	char *str="hello";
//	printf("%d\n",strlen(str));
//	return 0;
//}

//	strlen实现机理
int mylen(char *str);
int main(int argc,char *argcment) {
	char *str="hello world!";
	int length=	mylen(str);
	printf("%d",length);
}
int mylen(char *str) {
// char *str="hello";
	int index=0;
	while(str[index]!='\0') {
		index++;
	}
// printf("index=%d",index);
	return index;
}

2.strcmp()

在这里插入代码片
#include <stdio.h>
#include <string.h>

//直接调用string的strcmp()函数
int main(int argc,char *argv[]){
	char *a="hello ";
	char *b="hello";
	printf("%d",strcmp(a,b));
	
	return 0;
} 

//实现机理
//int mycmp(char *a,char *b);
//int main(int argc,char *argv[]) {
//	char *a="hello ";
//	char *b="hello";
//	int ret=mycmp(a,b);
//	if(ret==0) {
//		printf("same");
//	} else if(ret>0) {
//		printf("a more big");
//	} else {
//		printf("b more big");
//	}
//	return 0;
//}
//int mycmp(char *a,char *b) {
//	//method 1,利用数组的方式
	int idx=0;
	while(a[idx]==b[idx]&&a[idx]!=0){
		idx++;
	}
	return a[idx]-b[idx];
//
method 2,利用指针的方式
//	while(*a==*b &&*a!='\0') {
//		a++;
//		b++;
//	}
//	return *a-*b;
//}

3.strcpy()

在这里插入代码片
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//实现机理 
char* mystr(char* dst,char* str) {
	int idx=0;
//	while(str[idx]!='\0'){
//		dst[idx]=str[idx];
//		idx++;
//	}
//	dst[idx]='\0';
//	return dst;
//指针
	char *rest=dst;
	while(*str!='\0') {
		*dst=*str;
		str++;
		dst++;
	}
	*dst='\0';
	return rest;
}

int main(int argc,char const *argv[]) {

	char* s1="abc";
	char* s2=(char*)malloc(strlen(s1)+1);
	mystr(s2,s1);
	printf("%s",s2);
	free(s2);
	return 0;
}

4.strcat()

在这里插入代码片
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//int main(int argc,char* argv[]){
//	//注意s1的空间必须足够大 
//	char s1[10]="abc";
//	char* s2="-hello";
//	strcat(s1,s2);
//	printf("%s",s1);
//	return 0;
//}

//实现机理 
char *mycat(char* s1,char*s2);
int main(int argc,char* argv[]){
	char s1[20]="hello";
	char* s2="abcdef";
	mycat(s1,s2);
	printf("%s",s1);
	return 0;
} 

char *mycat(char* s1,char*s2){
	//数组 
//	int idx=0;
//	while(s1[idx]!='\0'){
//		idx++;
//	}
//	int idx2=0;
//	while(s1[idx2]!='\0'){
//		s1[idx]=s2[idx2];
//		idx++;
//		idx2++;
//	}
//	return s1;

//指针
char* ret=s1;
//s1的长度是5,strlen(s1)=5,也就是s2添加到hello后面即复制后是helloabcdef
//strcpy(s1+strlen(s1),s2);中的s1+strlen(s1)将要复制的字符串从s1+5位置开/始放置
//如果是strcpy(s1,s2);则s1变成s2了,就不是连接了而是替换了
strcpy(s1+strlen(s1),s2);
return ret; 
}

5.strchr()

在这里插入代码片
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc,char *argv[]) {
	char s[]="hello";
	char *p=strchr(s,'l');
	
	//正常获取 
	printf("%s\n",p);
	
	//获取he;
	char c=*p;
	*p='\0';
	char *t=(char*)malloc(strlen(s)+1);
	strcpy(t,s);
	printf("%s",t);
	free(t);
	*p=c;
	
	// 
	return 0;
}
  • 3
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值