strcspn(返回字符串中连续不含指定字符串内容的字符数)
相关函数
strspn
表头文件
#inclued<string.h>
定义函数
size_t strcspn ( const char *s,const char * reject);
函数说明
strcspn()从参数s字符串的开头计算连续的字符,而这些字符都完全不在参数reject 所指的字符串中。简单地说,若strcspn()返回的数值为n,则代表字符串s开头连续有n个字符都不含字符串reject内的字符。
返回值
返回字符串s开头连续不含字符串reject内的字符数目。
范例
#include <string.h>
main()
{
char *str="Linux was first developed for 386/486-based pcs.";
printf("%d\n",strcspn(str," "));
printf("%d\n",strcspn(str,"/-"));
printf("%d\n",strcspn(str,"1234567890"));
}
执行
5 /*只计算到“ ”的出现,所以返回“Linux”的长度*/
33 /*计算到出现“/”或“-”,所以返回到“6”的长度*/
30 /* 计算到出现数字字符为止,所以返回“3”出现前的长度*/
strdup(复制字符串)
相关函数
calloc,malloc,realloc,free
表头文件
#include<string.h>
定义函数
char * strdup( const char *s);
函数说明
strdup()会先用maolloc()配置与参数s字符串相同的空间大小,然后将参数s字符串的内容复制到该内存地址,然后把该地址返回。该地址最后可以利用free()来释放。
返回值
返回一字符串指针,该指针指向复制后的新字符串地址。若返回NULL表示内存不足。
范例
#include<string.h>
main()
{
char a[]="strdup";
char *b;
b=strdup(a);
printf("b[ ]=\"%s\"\n",b);
}
执行
b[ ]="strdup"
strlen(返回字符串长度)
相关函数
表头文件
#include<string.h>
定义函数
size_t strlen (const char *s);
函数说明
strlen()用来计算指定的字符串s的长度,不包括结束字符"\0"。
返回值
返回字符串s的字符数。
范例
/*取得字符串str的长度*/
#include<string.h>
main()
{
char *str = "12345678";
printf("str length = %d\n", strlen(str));
}
执行
str length = 8
