C语言字符串常用函数

strlen

函数原型:int strlen(const char*s)

 char *a="hall";//定型字符串 
 char *b="hello";
 char c[9]="hello";//灵活字符串 
 char d[]="ab";
 printf("sizeof(a)=%d\n",sizeof(a));	//sizeof(a)=8
 printf("strlen(a)=%d\n",strlen(a));	//strlen(a)=4
 printf("sizeof(c)=%d\n",sizeof(c));	//sizeof(c)=9
 printf("strlen(c)=%d\n",strlen(c));	//strlen(c)=5 
 printf("sizeof(d)=%d\n",sizeof(d));	//sizeof(d)=3  
 printf("strlen(d)=%d\n",strlen(d));	//strlen(d)=2

strcmp

函数原型:int strcmp(const char *s1,const char *s2)

 char *a="hall";//定型字符串 
 char *b="hello";
 char c[9]="hello";//灵活字符串 
 char d[]="ab";
 
 printf("strcmp(a,b)=%d\n",strcmp(a,b));   	//strcmp(a,b)=-1
 printf("strcmp(c,a)=%d\n",strcmp(c,a));   	//strcmp(c,a)=1
 printf("strncmp(c,b,3)=%d\n",strncmp(c,b,3)); //strncmp(c,b,3)=0

strcpy

函数原型:char *strcpy(char *dst,const char *src)

 char *a="hall";//定型字符串 
 char *b="hello";
 char c[9]="hello";//灵活字符串 
 char d[]="ab";
 
 printf("strcpy(c,a)=%s\n",strcpy(c,a));   	//strcpy(c,a)=hall
 printf("c=%s\n",c);        			//c=hall
 //printf("strcpy(a,c)=%s\n",strcpy(a,c));  	//报错 
 //printf("a=%s\n",a);       			//报错 
 printf("strncpy(c,b,2)=%s\n",strncpy(c,b,2)); 	//strncpy(c,b,2)=hell
 printf("c=%s\n",c);        			//c=hell

strcat

函数原型:char *strcat(char *s1,const char *s2)

 char *a="hall";//定型字符串 
 char *b="hello";
 char c[9]="hello";//灵活字符串 
 char d[]="ab";
 
 printf("strcat(c,d)=%s\n",strcat(c,d));   	//strcat(c,d)=helloab
 printf("c=%s\n",c);        			//c=helloab 
 printf("strncat(c,d,1)=%s\n",strncat(c,d,1)); 	//strncat(c,d,1)=helloaba 
 printf("c=%s\n",c);        			//c=helloaba 
 
     

strchr

函数原型:char *strchr(const char *s,char c)

 char *a="hall";//定型字符串 
 char *b="hello";
 char c[9]="hello";//灵活字符串 
 char d[]="ab";
 
 printf("strchr(c,'l')-c=%d\n",strchr(c,'l')-c);   	//strchr(c,'l')-c=2
 printf("strrchr(c,'l')-c=%d\n",strrchr(c,'l')-c);  	//strrchr(c,'l')-c=3 
 

strstr

函数原型:char*strstr(const char *s1,const char *s2)

 char*s1="helloabc";
 char*s2="ab";
 char a[]="ello";
 char b[]="ll";
 
 printf("strstr(s1,s2)=%s\n",strstr(s1,s2));	//strstr(s1,s2)=abc
 printf("strstr(s1,a)=%s\n",strstr(s1,a));	//strstr(s1,a)=elloabc
 printf("strstr(a,b)=%s\n",strstr(a,b));	//strstr(a,b)=llo
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值