string.h

返回

string.h, strspn

#include <stdio.h> #include <string.h> int main(void) { int n; n = strspn("abccccdxdeeee","abcde"); printf("%d\n", n); return 0; } /* 運(yùn)行結(jié)果 7 */

string.h, strpbrk

// 檢索 #include <stdio.h> #include <string.h> int main(void) { char *p; char s[] = "void func(char ss[])"; p = strpbrk(s, "()[]{}"); printf("%s\n", p); return 0; } /* 運(yùn)行結(jié)果 (char ss[]) */

string.h, strncpy

// 把文字列s2的指定長(zhǎng)度復(fù)寫(xiě)到文字列s1上 #include <stdio.h> #include <string.h> int main(void) { int i; char s1[10] = "ABCDEFGH"; char s2[10] = "1234"; strncpy(s1, s2, 2); printf("s1=[%s] s2=[%s]\n", s1, s2); for (i=0; i<8; i++) printf("%02X ", s1[i]); putchar('\n'); strncpy(s1, s2, 6); printf("s1=[%s] s2=[%s]\n", s1, s2); for (i=0; i<8; i++) printf("%02X ", s1[i]); putchar('\n'); return 0; } /* 運(yùn)行結(jié)果 s1=[12CDEFGH] s2=[1234] 31 32 43 44 45 46 47 48 s1=[1234] s2=[1234] 31 32 33 34 00 00 47 48 */

string.h, strncmp

#include <stdio.h> #include <string.h> int main(void) { char st[10] = "mainishere"; char s2[10] = "main OK!"; if ( strncmp(st, "main", 4) == 0 ) { printf("strncat=%s", s2); } return 0; } /* 運(yùn)行結(jié)果 strncat=main OK! */

string.h, strncat

#include <stdio.h> #include <string.h> int main(void) { char s1[10] = "abcde"; char s2[10] = "ABCDE"; strncat(s1, s2, 4); printf("strncat=%s", s1); return 0; } /* 運(yùn)行結(jié)果 strncat=abcdeABCD */

string.h, strlen

#include <stdio.h> #include <string.h> int main(void) { int n; char ss[80] = "This is a test for strlen."; n = strlen(ss); printf("strlen=%d", n); return 0; } /* 運(yùn)行結(jié)果 strlen=26 */

string.h, strerror

#include <stdio.h> #include <string.h> int main(void) { int i; for (i=0; i<=20; i++) printf("%d:%s\n", i, strerror(i)); return 0; } /* 運(yùn)行結(jié)果 0:No error 1:Operation not permitted 2:No such file or directory 3:No such process 4:Interrupted function call 5:Input/output error 6:No such device or address 7:Arg list too long 8:Exec format error 9:Bad file descriptor 10:No child processes 11:Resource temporarily unavailable 12:Not enough space 13:Permission denied 14:Bad address 15:Unknown error 16:Resource device 17:File exists 18:Improper link 19:No such device 20:Not a directory */

string.h, strcspn

#include <stdio.h> #include <string.h> int main(void) { int n; n = strcspn("127xxxxxxya567", "abcde"); printf("n=%d\n",n); return 0; } /* 運(yùn)行結(jié)果 n=10 */

string.h, strcpy

#include <stdio.h> #include <string.h> int main(void) { char s1[80]; strcpy(s1, "strcpy copy"); printf("s1=%s\n",s1); return 0; } /* 運(yùn)行結(jié)果 s1=strcpy copy */

string.h, strcmp

#include <stdio.h> #include <string.h> int main(void) { char s1[] = "zzz"; char s2[] = "xyz"; if (strcmp(s1, s2) == 0 ) { printf("s1 equals to s2\n"); } else if ( strcmp(s1, s2) > 0 ) { printf("s1 is bigger than s2\n"); } else { printf("s1 is smaller than s2\n"); } return 0; } /* 運(yùn)行結(jié)果 s1 is bigger than s2 */

string.h, strcoll

#include <stdio.h> #include <string.h> int main(void) { // char s1[] = "abc"; char s1[] = "xyz"; char s2[] = "xyz"; if (strcoll(s1, s2) == 0 ) { printf("s1 equals to s2\n"); } else { printf("s1 does not equal to s2\n"); } return 0; } /* 運(yùn)行結(jié)果 s1 does not equal to s2 s1 equals to s2 */

string.h, strchr

#include <stdio.h> #include <string.h> int main(void) { char *p; char st[] = "strchr-test"; p = strchr(st, 'h'); printf("st=%s\n", p); printf("pt=%c\n", *p); return 0; } /* 運(yùn)行結(jié)果 st=hr-test pt=h */

string.h,strcat

#include <stdio.h> #include <string.h> int main(void) { char st[80] = "B456"; strcat(st, "ABCDXYS"); printf("st=%s\n", st); return 0; } /* 運(yùn)行結(jié)果 st=B456ABCDXYS */

string.h,memcpy,memmove

#include <stdio.h> #include <string.h> int main(void) { int i; int dt1[5] = {10, 20, 30, 40, 50}; int dt2[5]; memcpy(dt2, dt1, sizeof(dt1)); for (i=0; i<=4; i++) printf("%d ", dt2[i]); putchar('\n'); return 0; } /* 運(yùn)行結(jié)果 10 20 30 40 50 */ #include <stdio.h> #include <string.h> int main(void) { char a1[10] = "abcdefghij"; char a2[10] = "xyz1234567"; // memcpy(a2, a1, 4); memmove(a2, a1, 4); printf("%s\n", a2); return 0; } /* 運(yùn)行結(jié)果 abcd234567 */

string.h,memchr

#include <stdio.h> #include <string.h> int main(void) { char *p, ss[] = "abcdefghij"; strcpy(ss,"123"); p = strchr(ss, 'f'); if (p == NULL) p = ""; printf("[%s]\n", p); p = memchr(ss, 'f', sizeof(ss)); // memchr的使用法 if (p == NULL) p = ""; printf("[%s]\n", p); return 0; } /* 運(yùn)行結(jié)果 [] [fghij] */

string.h,memcmp

#include <stdio.h> #include <string.h> int main(void) { int dt1[] = {10, 20, 30, 40, 50}; int dt2[] = {10, 20, 30, 40, 50}; int dt3[] = {11, 20, 30, 40, 50}; if (memcmp(dt1, dt2, sizeof(int)*5) == 0 ) puts("first equal"); else puts("first not equal"); if (memcmp(dt1, dt3, sizeof(int)*5) == 0 ) puts("second equal"); else puts("second not equal"); return 0; } /* 運(yùn)行結(jié)果 first equal second not equal */
返回
隆林| 察哈| 汽车| 株洲县| 庆元县| 镇远县| 聂拉木县| 洪江市| 海丰县| 红原县| 绥中县| 伽师县| 辽阳市| 元谋县| 安溪县| 海伦市| 乐陵市| 岱山县| 陵水| 金寨县| 灵寿县| 达州市| 农安县| 房产| 鹰潭市| 宁强县| 清苑县| 曲水县| 财经| 小金县| 天长市| 祁门县| 信宜市| 驻马店市| 洛扎县| 临城县| 海城市| 无极县| 辽宁省| 灵寿县| 庄河市|