c语言字符串转函数名 c语言中把字符串转换为数值的函数( 三 )


#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);
printf("The string is %s the number is %lf/n", input, value);
return 0;
}
函数名: strtol
功能: 将串转换为长整数
用法: long strtol(char *str, char **endptr, int base);
举例:
[cpp] view plain copy
#include stdlib.h
#include stdio.h
int main(void)
{
char *string = "87654321", *endptr;
long lnumber;
/* strtol converts string to long integer*/

推荐阅读