c语言汉字的函数库 c语言汉字的数据类型( 四 )


}
函数名: 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
功能: 返回指向错误信息字符串的指针
用法: char *strerror(int errnum);
举例:
[cpp] view plain copy
#include stdio.h
#include errno.h
int main(void)
{
char *buffer;
buffer = strerror(errno);
printf("Error: %s/n", buffer);
return 0;
}
函数名: strncmp
功能: 串比较
用法: int strncmp(char *str1, char *str2, int maxlen);
举例:
[cpp] view plain copy
#include string.h
#include stdio.h
intmain(void)
{
char *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc";
int ptr;
ptr = strncmp(buf2,buf1,3);
if (ptr0)
printf("buffer 2 is greater than buffer 1/n");
else
printf("buffer 2 is less than buffer 1/n");
ptr = strncmp(buf2,buf3,3);
if (ptr0)
printf("buffer 2 is greater than buffer 3/n");
else
printf("buffer 2 is less than buffer 3/n");
return(0);
}
函数名: strncmpi
功能: 把串中的一部分与另一串中的一部分比较, 不管大小写
用法: int strncmpi(char *str1, char *str2, int len);
举例:
[cpp] view plain copy
#include string.h
#include stdio.h
int main(void)
{
char *buf1 = "BBBccc", *buf2 = "bbbccc";
int ptr;
ptr = strncmpi(buf2,buf1,3);
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;
}
函数名: strnset
功能: 将一个串中的所有字符都设为指定字符
用法: char *strnset(char *str, char ch, unsigned n);
举例:
[cpp] view plain copy
#include stdio.h
#include string.h
int main(void)
{
char *string = "abcdefghijklmnopqrstuvwxyz";
char letter = 'x';
printf("string before strnset: %s/n", string);
strnset(string, letter, 13);
printf("string afterstrnset: %s/n", string);
return 0;
}
函数名: strpbrk
功能: 在串中查找给定字符集中的字符
用法: char *strpbrk(char *str1, char *str2);
举例:
[cpp] view plain copy
#include stdio.h
#include string.h
int main(void)
{
char *string1 = "abcdefghijklmnopqrstuvwxyz";
char *string2 = "onm";
char *ptr;
ptr = strpbrk(string1, string2);
if (ptr)
printf("strpbrk found first character: %c/n", *ptr);
else
printf("strpbrk didn't find character in set/n");
return 0;
}
函数名: strrev
功能: 串倒转
用法: char *strrev(char *str);
举例:
[cpp] view plain copy
#include string.h
#include stdio.h
int main(void)
{
char *forward = "string";
printf("Before strrev(): %s/n", forward);

推荐阅读