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


功能: 返回指向错误信息字符串的指针
用法: 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);
strrev(forward);
printf("After strrev():%s/n", forward);
return 0;
}
/*运行结果:
Before strrev(): string
After strrev():gnirts
*/
函数名: strstr
功能: 在串中查找指定字符串的第一次出现
用法: char *strstr(char *str1, char *str2);
举例:
[cpp] view plain copy
#include stdio.h
#include string.h
int main(void)
{
char *str1 = "Borland International", *str2 = "nation", *ptr;
ptr = strstr(str1, str2);
printf("The substring is: %s/n", ptr);
return 0;
}
函数名: strtod
功能: 将字符串转换为double型值
用法: double strtod(char *str, char **endptr);
举例:
[cpp] view plain copy
#include stdio.h
#include stdlib.h
int main(void)
{
char input[80], *endptr;
double value;
printf("Enter a floating point number:");
gets(input);
value = https://www.04ip.com/post/strtod(input, endptr);

推荐阅读