string.h
返回
string.h, strspn
#include
#include
int main(void)
{
int n;
n = strspn("abccccdxdeeee","abcde");
printf("%d\n", n);
return 0;
}
/*
运行结果
7
*/
string.h, strpbrk
// 检索
#include
#include
int main(void)
{
char *p;
char s[] = "void func(char ss[])";
p = strpbrk(s, "()[]{}");
printf("%s\n", p);
return 0;
}
/*
运行结果
(char ss[])
*/
string.h, strncpy
// 把文字列s2的指定长度复写到文字列s1上
#include
#include
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;
}
/*
运行结果
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
#include
int main(void)
{
char st[10] = "mainishere";
char s2[10] = "main OK!";
if ( strncmp(st, "main", 4) == 0 ) {
printf("strncat=%s", s2);
}
return 0;
}
/*
运行结果
strncat=main OK!
*/
string.h, strncat
#include
#include
int main(void)
{
char s1[10] = "abcde";
char s2[10] = "ABCDE";
strncat(s1, s2, 4);
printf("strncat=%s", s1);
return 0;
}
/*
运行结果
strncat=abcdeABCD
*/
string.h, strlen
#include
#include
int main(void)
{
int n;
char ss[80] = "This is a test for strlen.";
n = strlen(ss);
printf("strlen=%d", n);
return 0;
}
/*
运行结果
strlen=26
*/
string.h, strerror
#include
#include
int main(void)
{
int i;
for (i=0; i<=20; i++)
printf("%d:%s\n", i, strerror(i));
return 0;
}
/*
运行结果
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
#include
int main(void)
{
int n;
n = strcspn("127xxxxxxya567", "abcde");
printf("n=%d\n",n);
return 0;
}
/*
运行结果
n=10
*/
string.h, strcpy
#include
#include
int main(void)
{
char s1[80];
strcpy(s1, "strcpy copy");
printf("s1=%s\n",s1);
return 0;
}
/*
运行结果
s1=strcpy copy
*/
string.h, strcmp
#include
#include
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;
}
/*
运行结果
s1 is bigger than s2
*/
string.h, strcoll
#include
#include
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;
}
/*
运行结果
s1 does not equal to s2
s1 equals to s2
*/
string.h, strchr
#include
#include
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;
}
/*
运行结果
st=hr-test
pt=h
*/
string.h,strcat
#include
#include
int main(void)
{
char st[80] = "B456";
strcat(st, "ABCDXYS");
printf("st=%s\n", st);
return 0;
}
/*
运行结果
st=B456ABCDXYS
*/
string.h,memcpy,memmove
#include
#include
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;
}
/*
运行结果
10 20 30 40 50
*/
#include
#include
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;
}
/*
运行结果
abcd234567
*/
string.h,memchr
#include
#include
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;
}
/*
运行结果
[]
[fghij]
*/
string.h,memcmp
#include
#include
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;
}
/*
运行结果
first equal
second not equal
*/
返回