函数名: strpbrk 功 能: 在串中查找给定字符集中的字符 用 法: char *strpbrk(char *str1, char *str2); 程序例: #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
", *ptr); else printf("strpbrk didn"t find character in set
"); return 0; }
函数名: strrchr 功 能: 在串中查找指定字符的最后一个出现 用 法: char *strrchr(char *str, char c); 程序例: #include <string.h> #include <stdio.h> int main(void) { char string[15]; char *ptr, c = "r"; strcpy(string, "This is a string"); ptr = strrchr(string, c); if (ptr) printf("The character %c is at position: %d
", c, ptr-string); else printf("The character was not found
"); return 0; }
函数名: strtod 功 能: 将字符串转换为double型值 用 法: double strtod(char *str, char **endptr); 程序例: #include <stdio.h> #include <stdlib.h> int main(void) { char input[80], *endptr; double value; printf("Enter a floating point number:"); gets(input); value = strtod(input, &endptr); printf("The string is %s the number is %lfn", input, value); return 0; }
函数名: strsep 功 能: 分解字符串为一组字符串。从str1指向的位置起向后扫描,遇到delim指向位置的字符后,将此字符替换为NULL,返回str1指向的地址。 用 法: char *strtok(char **str1, const char *delim); 程序例: int main() { int len, nel; char query[] ="user_command=appleboy&test=1&test2=2"; char *q, *name, *value; /* Parse into individualassignments */ q = query; fprintf(stderr, "CGI[query string] : %s
",query); len = strlen(query); nel = 1; while (strsep(&q, "&")) nel++; fprintf(stderr, "CGI[nel string] : %d
", nel); for (q = query; q< (query + len);) { value = name = q; /* Skip to next assignment */ fprintf(stderr, "CGI[string] :%s
", q); fprintf(stderr, "CGI[stringlen] : %d
", strlen(q)); fprintf(stderr, "CGI[address] :%x
", q); for (q += strlen(q); q < (query +len) && !*q; q++); /* Assign variable */ name = strsep(&value,"="); fprintf(stderr, "CGI[name ] :%s
", name); fprintf(stderr, "CGI[value] :%s
", value); } return 0; }
函数名: strtok 功 能: 查找由在第二个串中指定的分界符分隔开的单词 用 法: char *strtok(char *str1, char *str2); 程序例: #include <string.h> #include <stdio.h> int main(void) { char input[16] = "abc,d"; char *p; /* strtok places a NULL terminator in front of the token, if found */ p = strtok(input, ","); if (p) printf("%sn", p); /* A second call to strtok using a NULL as the first parameter returns a pointer to the character following the token */ p = strtok(NULL, ","); if (p) printf("%sn", p); return 0; }
函数名: strtol 功 能: 将串转换为长整数 用 法: long strtol(char *str, char **endptr, int base); 程序例: #include <stdlib.h> #include <stdio.h> int main(void) { char *string = "87654321", *endptr; long lnumber; /* strtol converts string to long integer */ lnumber = strtol(string, &endptr, 10); printf("string = %s long = %ldn", string, lnumber); return 0; }