c语言字符串匹配库函数 c语言字符串查找函数( 三 )


#include string.h
main()
{
char buf[4];
char *s="abcdefg";
strncpy(buf,s,4);
printf("%s/n",buf);
return 0;
}
/*运行结果:
abcd
*/
函数名: stpcpy
功能: 拷贝一个字符串到另一个
用法: char *stpcpy(char *destin, char *source);
举例:
[cpp] view plain copy
#include stdio.h
#include string.h
int main(void)
{
char string[10];
char *str1 = "abcdefghi";
stpcpy(string, str1);
printf("%s/n", string);
return 0;
}
/*运行结果
abcdefghi
*/
函数名: strcat
功能: 字符串拼接函数
用法: char *strcat(char *destin, char *source);
举例:
[cpp] view plain copy
#include string.h
#include stdio.h
int main(void)
{
char destination[25];
char *blank = " ", *c = "C++", *Borland = "Borland";
strcpy(destination, Borland);
strcat(destination, blank);
strcat(destination, c);
printf("%s/n", destination);
return 0;
}
/*运行结果:
Borland C++
*/
函数名: strcmp
功能: 串比较
用法: int strcmp(char *str1, char *str2);
看Asic码,str1str2,返回值0;两串相等 , 返回0
举例:
[cpp] view plain copy
#include string.h
#include stdio.h
int main(void)
{
char *buf1 = "aaa", *buf2 = "bbb";
int ptr;
ptr = strcmp(buf2, buf1);
if (ptr0)
printf("buffer 2 is greater than buffer 1/n");
else if(ptr0)
printf("buffer 2 is less than buffer 1/n");
else
printf("buffer 2 is equal with buffer 1/n");
return 0;
}
/*运行结果:
buffer 2 is greater than buffer 1
*/
函数名: strncmpi
功能: 将一个串中的一部分与另一个串比较, 不管大小写
用法: int strncmpi(char *str1, char *str2, unsigned maxlen);
举例:
[cpp] view plain copy
#include string.h
#include stdio.h
int main(void)
{
char *buf1 = "BBB", *buf2 = "bbb";
int ptr;
ptr = strcmpi(buf2, buf1);
if (ptr0)
printf("buffer 2 is greater than buffer 1/n");
if (ptr0)
printf("buffer 2 is less than buffer 1/n");
if (ptr == 0)
printf("buffer 2 equals buffer 1/n");
return 0;
}
函数名: strcspn
功能: 在串中查找第一个给定字符集内容的段
用法: int strcspn(char *str1, char *str2);
举例:
[cpp] view plain copy
#include stdio.h
#include string.h
#include alloc.h
int main(void)
{
char *string1 = "1234567890";
char *string2 = "747DC8";
int length;
length = strcspn(string1, string2);
printf("Character where strings intersect is at position %d/n", length);
return 0;
}
函数名: strdup
功能: 将串拷贝到新建的位置处
用法: char *strdup(char *str);
举例:
[cpp] view plain copy
#include stdio.h
#include string.h
#include alloc.h
int main(void)
{
char *dup_str, *string = "abcde";
dup_str = strdup(string);
printf("%s/n", dup_str);
free(dup_str);
return 0;
}
函数名: stricmp
功能: 以大小写不敏感方式比较两个串
用法: int stricmp(char *str1, char *str2);
举例:
[cpp] view plain copy
#include string.h
#include stdio.h
int main(void)
{
char *buf1 = "BBB", *buf2 = "bbb";
int ptr;
ptr = stricmp(buf2, buf1);
if (ptr0)
printf("buffer 2 is greater than buffer 1/n");
if (ptr0)
printf("buffer 2 is less than buffer 1/n");
if (ptr == 0)
printf("buffer 2 equals buffer 1/n");
return 0;
}
函数名: strerror

推荐阅读