1.strlen字符测量函数
头文件:#include<string.h>
函数定义:size_t strlen(const char *s)
功能:测字符指针s指向的字符串中字符的个数,到’\0’结束且不包括’\0’
返回值:测量的个数
例子:
#include<stdio.h>
#include<string.h>
void test1()
{
char s1[16] = "holle world";
char s2[] = "holle world";
char s3[] = "holle wo\0rld";
printf("s1 = %d\n",sizeof(s1));
printf("s1 = %d\n",sizeof(s2));
printf("s1 = %d\n",sizeof(s3));
printf("s1 = %d\n",strlen(s1));
printf("s1 = %d\n",strlen(s2));
printf("s1 = %d\n",strlen(s3));
}
运行结果:
s1 = 16
s1 = 12
s1 = 13
s1 = 11
s1 = 11
s1 = 8
2 .strcpy字符拷贝函数
头文件:#include<string.h>
函数定义:char *strcpy(char *dest,const char *src)
功能:拷贝src指向的字符串拷贝到dest指向的内存中,’\0’也会拷贝
返回值:目的内存的地址
注意:dest的空间要足够大
例子:
void test2()
{
char src[] = "hello wor\0ld";
char dest[32] = "";
strcpy(dest,src);
printf("dest = %s\n",dest);
}
运行结果:
dest = hello wor
3.strncpy字符拷贝函数
头文件:#include<string.h>
函数定义:char *strncpy(char *dest,const char *src,size_t n)
功能:拷贝src指向的字符串的前n个拷贝到dest指向的内存中,’\0’也不会拷贝
返回值:目的内存的地址
如果n大于src指向的字符串中的字符个数,则dest后面自动补n-strlen(src)个’\0’
例子:
void test2()
{
char src[] = "hello wor\0ld";
char dest[32] = "";
strncpy(dest,src,3);
printf("dest = %s\n",dest);
}
运行结果:
dest = hel
4.strcat字符追加函数
头文件:#include<string.h>
函数定义:char *strcat(char *dest,const char *src)
功能:追加src字符串到dest指向的字符串后面,’\0’也会被追加
返回值:目的内存的地址
注意:dest的空间要足够大
例子:
void test2()
{
char src[] = "hello ";
char dest[32] = "world";
strcat(dest,src);
printf("dest = %s\n",dest);
}
运行结果:
dest = world hello
5.strncat字符串追加函数
头文件:#include<string.h>
函数定义:char *strcat(char *dest,const char *src,size_t n)
功能:追加src的前n个字符串到dest指向的字符串后面
返回值:目的内存的地址
注意:dest的空间要足够大
例子:
void test2()
{
char src[] = "hello";
char dest[32] = " world";
strncat(dest,src,3);
printf("dest = %s\n",dest);
}
dest = world hel
6 strcmp字符串比较函数
头文件:#include<string.h>
函数定义:int strcmp(const char *s1,const char *s2)
功能:逐一比较s1和s2指向的字符串中的字符的ascII码
返回值:如果s1指向的字符串大于s2指向的字符串,返回>0
如果s1指向的字符串小于s2指向的字符串,返回<0
如果s1指向的字符串等于s2指向的字符串,返回=0
例子:
void test3()
{
char s1[] = "holle";
char s2[] = "holie";
int n = strcmp(s1,s2);
printf("n = %d\n",n);
}
运行结果:
n=3
7.strncmp字符串比较函数
头文件:#include<string.h>
函数定义:int strcmp(const char *s1,const char *s2,size_t n)
功能:逐一比较s1和s2指向的字符串中的前n个字符的ascII码
返回值:如果s1指向的字符串大于s2指向的字符串,返回>0
如果s1指向的字符串小于s2指向的字符串,返回<0
如果s1指向的字符串等于s2指向的字符串,返回=0
例子:
void test3()
{
char s1[] = "holle";
char s2[] = "holze";
int n = strncmp(s1,s2,4);
printf("n = %d\n",n);
}
运行结果:
n = -14
8.strchr字符查找函数
头文件:#include<string.h>
函数定义:char *strchr(const char *s,int c)
功能:在字符指针s指向的字符串中,查找asccII为c的字符串,如果s指向的字符串中有多个ascII为c的字符,则找的是第一个
返回值:找到了返回找到的字符的地址
找不到返回NULL
例子:
void test3()
{
char s1[] = "holle";
printf("s1 = %s\n",strchr(s1,'l'));
}
运行结果:
s1 = lle
9.strstr字符串查找
头文件:#include<string.h>
char *strstr(const str *haystack,const char *needle)
功能:在haystack指向的字符串中查找needle指向的字符串,也是找第一个
返回值:找到返回字符串的首地址
没找到返回NULL
例子:
void test4()
{
char str[] = "hello world";
while(1)
{
char *tmp = strstr(str,"l");
if(tmp != NULL)
{
memset(tmp,"*",strlen("l"));
}
else
{
break;
}
}
printf("str = %s\n",str);
}
运行结果:
str = he��o wor�d
10strtok字符串切割函数
头文件:#include<string.h>
函数定义:char *strtok(const char *str,char *delim)
功能:按照delim指向的字符串中的字符,切割str指向的字符串。如果在str指向的字符串中发现了delim字符串中的字符,就将其变成’\0’,调用一次strtok就只切割一次,切割过后,再去切割就要把strtok的第一个参数传入NULL,意思是接着上一次的切割位置继续切割
void test5()
{
char str[] = "sijie:world?nihao,hello";
char *buf[16]= {NULL};
int i = 0;
buf[i] = strtok(str,":?,");
while(buf[i] != NULL)
{
i++;
buf[i] = strtok(NULL,":?,");
}
i = 0;
while(buf[i] != NULL)
{
printf("buf[%d] = %s\n",i,buf[i]);
i++;
}
}
运行结果:
buf[0] = sijie
buf[1] = world
buf[2] = nihao
buf[3] = hello