例1:
# include <stdio.h> # include <string.h> void judge(char * p); int main(void) { char * s = "Happy New Year!"; char * s1 = "ew"; char * s2 = "aa"; char * p1, * p2; p1 = strstr(s, s1); p2 = strstr(s, s2); judge(p1); judge(p2); return 0; } void judge(char * p) { if(p) printf("%s\n", p); else printf("Not Found!\n"); }输出结果为:
# include <stdio.h> # include <stdlib.h> int main(void) { char * p; char s1[] = "123.4567"; char s2[] = "1234.567bxs"; char s3[] = "-12.34567"; char s4[] = "123.4567e3"; printf("s1=%lf\n", strtod(s1, NULL)); printf("s2=%lf\n", strtod(s2, &p)); printf("p=%s\n", p); printf("s3=%lf\n", strtod(s3, NULL)); printf("s4=%lf\n", strtod(s4, NULL)); return 0; }
输出结果为:
# include <stdio.h> # include <string.h> void judge(char * result, char * str, char * delims); int main(void) { char str1[] = "It's #a nice# day today."; char str2[] = "The domestic operating system!"; char delims[] = "#"; char * result = NULL; judge(result, str1, delims); printf("\n"); judge(result, str2, delims); return 0; } void judge(char * result, char * str, char * delims) { result = strtok(str, delims); while(result != NULL) { printf("result is \"%s\"\n", result); result = strtok(NULL, delims); } }输出结果为:
# include <stdio.h> # include <stdlib.h> int main(void) { char * p; char s1[] = "34"; char s2[] = "48asd"; char s3[] = "-56"; char s4[] = "12e3"; char s5[] = "0x21";//如果像这样加0x,那就是指定了是十六进制数 printf("s1=%ld\t", strtol(s1, NULL, 10)); printf("s1=%ld\n\n", strtol(s1, NULL, 8)); printf("s2=%ld\t", strtol(s2, &p, 10)); printf("p=%s\n\n", p); printf("s3=%ld\t", strtol(s3, 0, 10)); printf("s3=%ld\n\n", strtol(s3, 0, 8)); printf("s4=%ld\t", strtol(s4, NULL, 10)); printf("s4=%ld\n\n", strtol(s4, NULL, 8)); printf("s5=%ld\t", strtol(s5, NULL, 0));//如果最后的参数是0,则表示自动识别进制 return 0; }
输出结果为:
unsigned long strtoul(const char * start, char ** end, int base);
功能:与strtol函数相似,不同的是,它不仅可以返回长整型数,而且可以返回无符号的长整型数。
这里就不举例了
size_t strxfrm(char * str1, const char * str2, size_t num);
功能:函数将字符串str2 的前num 个字符存储到字符串str1中,其返回值是str2的字符串长度(不包含\0)。
例5:
# include <stdio.h> # include <string.h> int main(void) { char s[20]; char s1[] = "34asd"; size_t t1 = strxfrm(s, s1, 10); size_t t2 = strxfrm(s, s1, 3); printf("%lu\n", t1); printf("%lu\n", t2); printf("%s\n", s); return 0; }输出结果为:
所有程序均在ubuntu12.04下codeblocks10.05下运行通过
如有错误,欢迎指出