前言:
学习这些库函数大概分为以下几步。 0、在MSDN中学习库函数的基本语法。1、库函数的代码实现方便理解与运用。2、库函数的注意事项。3、库函数的模拟实现。
- 代码块最后数字为代码运行答案。
一、字符串长度 函数-----strlen
size-t strlen(const char string);
#include <stdio.h>
#include <string.h>
int main()
{
char arr[20] = "hello world";
int len = strlen(arr);
printf("len=%d ", len);
return 0;//len=11
}
- 字符串以’\0’作为函数的结束标志,strlen计算的是’\0’之前出现字符的个数(不包括‘\0’)。
- 函数的返回值是无符号的size-t。
模拟实现、
#include <stdio.h>
#include <string.h>
size_t my_strlen(const char* parr)
{
size-t count = 0;
while (*parr != '\0')
{
count++;
parr++;
}
return count;
}
int main()
{
char arr[20] = "hello world";
int len = my_strlen(arr);
printf("len=%d ", len);
return 0;//len=11
}
二、长度不受限制的字符串函数
0、字符串拷贝函数-----strcpy
char *strcpy( char strDestination, const char strSource );
#include <stdio.h>
int main()
{
char arr1[20] = { 0 };
char arr2[20] = "hello world";
strcpy(arr1, arr2);
printf("%s ", arr1);
return 0;//hello world
}
- 源字符串必须以 ‘\0’ 结束。
- 会将源字符串中的 ‘\0’ 拷贝到目标空间。
- 目标空间必须足够大,以确保能存放源字符串。
- 目标空间必须可变。**
模拟实现、
#include <stdio.h>
char* my_strcpy(char* dest, char* src)
{
char* tmp = dest;
while (*src != '\0')
{
*dest++ = *src++;
}
return tmp;
}
int main()
{
char arr1[20] = { 0 };
char arr2[20] = "hello world";
my_strcpy(arr1, arr2);
printf("%s ", arr1);
return 0;//hello world
}
1、字符串追加函数-----strcat
char *strcat( char strDestination, const char strSource );
#include <stdio.h>
int main()
{
char arr1[20] = "hello ";
char arr2[20] = "word";
strcat(arr1, arr2);
printf("%s", arr1);
return 0;//hello world
}
- 源字符串必须以 ‘\0’ 结束。
- 目标空间必须有足够的大,能容纳下源字符串的内容。
- 目标空间必须可修改。
模拟实现、
#include <stdio.h>
#include <assert.h>
char* my_strcat(char* dest, const char* src)
{
assert(dest != NULL);
assert(src != NULL);
char* tmp = dest;
while (*dest != '\0')
{
dest++;
}
while (*dest++ = *src++)
{
;
}
return tmp;
}
int main()
{
char arr1[20] = "hello ";
char arr2[20] = "word";
my_strcat(arr1, arr2);
printf("%s", arr1);
return 0;//hello world
}
2、字符串比较函数-----strcmp
int strcmp( const char string1, const char string2 );
#include <stdio.h>
int main()
{
char arr1[] = "hello world";
char arr2[] = "hello world";
int len = strcmp(arr1, arr2);
printf("len = %d ", len);
return 0;//len = 0;
}
- 当第一个字符串小于第二个字符串,返回一个小于零的数字。
- 当第一个字符串等于第二个字符串,返回零。
- 当第一个字符串大于第二个字符串,返回一个大于零的数字。
- 两个字符比较,比较的是ASCII值的大小。
模拟实现、
#include <stdio.h>
#include <assert.h>
int my_strcmp(char* dest, char* src)
{
assert(dest != NULL);
assert(src != NULL);
while (*dest != '\0' && *src != '\0' && *dest == *src)
{
dest++;
src++;
}
if (*dest == '\0' && *src == '\0')
return 0;
else if (*dest < *src)
return -1;
else
return 1;
}
int main()
{
char arr1[] = "hello world";
char arr2[] = "hello world";
int len = my_strcmp(arr1, arr2);
printf("len = %d ", len);
return 0;//len = 0
}
三、长度受限制的字符串函数
0、字符串拷贝函数-----strncpy
char *strncpy( char strDest, const char strSource, size_t count );
int main()
{
char arr1[20] = { 0 };
char arr2[20] = "hello world";
strncpy(arr1, arr2, 11);
printf("%s", arr1);
return 0;//hello world
}
- 拷贝num个字符从源字符串到目标空间。
- 如果源字符串的长度小于num,则拷贝完源字符串之后,在目标的后边追加0,直到num个。
模拟实现、
#include <stdio.h>
#include <assert.h>
char* my_strncpy(char* dest, const char* src, size_t num)
{
assert(dest != NULL);
assert(src != NULL);
char* tmp = dest;
while (num--)
{
*dest++ = *src++;
}
return tmp;
}
int main()
{
char arr1[20] = { 0 };
char arr2[20] = "hello world";
my_strncpy(arr1, arr2, 11);
printf("%s", arr1);
return 0;//hello world
}
1、字符串追加函数-----strncat
char *strncat( char strDest, const char strSource, size_t count );
- Appends the first num characters of source to destination, plus a terminating null-character.
- If the length of the C string in source is less than num, only the content up to the terminatingnull-character is copied.
模拟实现、
#include <stdio.h>
#include <assert.h>
char* my_strncat(char* dest, char* src, size_t num)
{
assert(dest != NULL);
assert(src != NULL);
char* tmp = dest;
while (*dest != '\0')
{
dest++;
}
while (num--)
{
*dest = *src;
dest++;
src++;
}
return tmp;
}
int main()
{
char arr1[20] = "hello ";
char arr2[20] = "world";
my_strncat(arr1, arr2, 5);
printf("%s", arr1);
return 0;//hello world
}
2、字符串比较函数-----strncmp
int strncmp( const char string1, const char string2, size_t count );
模拟实现、
#include <stdio.h>
#include <string.h>
#include <assert.h>
int my_strncmp(char* dest, char* src, size_t num)
{
assert(dest != NULL);
assert(src != NULL);
while (num--)
{
if (*dest == *src && *dest != '\0' && *src != '\0')
{
dest++;
src++;
}
if (*dest > *src)
return 1;
if (*dest < *src)
return -1;
}
return 0;
}
int main()
{
char arr1[20] = "hella world";
char arr2[20] = "hello world";
int len = my_strncmp(arr1, arr2, 5);
printf("len=%d", len);
return 0;//len=-1
}
四、字符串查找函数
0、在字符串中寻找子字符串-----strstr
char *strstr( const char *string, const char strCharSet );
#include <stdio.h>
#include <string.h>
int main()
{
char arr1[20] = "smk@www.com";
char arr2[20] = "www";
char* tmp = strstr(arr1, arr2);
printf("%s", tmp);
return 0;//www.com
}
- Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.
模拟实现、
#include <stdio.h>
#include <string.h>
#include <assert.h>
char* my_strstr(char* dest, char* src)
{
assert(dest != NULL);
assert(src != NULL);
char* tmp = dest;
char* s1 = dest;
char* s2 = src;
while (*tmp != '\0')
{
s1 = tmp;
s2 = src;
while (*s1 == *s2 && *s1 != '\0' && *s2 != '\0')
{
s1++;
s2++;
}
if (*s2 == '\0')
return tmp;
tmp++;
}
return NULL;
}
int main()
{
char arr1[20] = "smk@www.com";
char arr2[20] = "www";
char* tmp = my_strstr(arr1, arr2);
printf("%s", tmp);
return 0;//www.com
}
1、分割字符串-----strtok
char * strtok ( char * str, const char * sep );
#include <stdio.h>
#include <string.h>
int main()
{
char arr[20] = "smk@www.com";
char arr2[3] = { '@','.' };
char arr3[40] = {0};
strncpy(arr3,arr,20);
char* ret = NULL;
for (ret = strtok(arr1, arr2); ret != NULL; ret=strtok(NULL, arr2))
{
printf("%s", ret);
}
return 0;
}
//smk
//www
//com
- sep参数是个字符串,定义了用作分隔符的字符集合。
- 第一个参数指定一个字符串,它包含了0个或者多个由sep字符串中一个或者多个分隔符分割的标记。
- strtok函数找到str中的下一个标记,并将其用 \0 结尾,返回一个指向这个标记的指针。(注:strtok函数会改变被操作的字符串,所以在使用strtok函数切分的字符串一般都是临时拷贝的内容并且可修改。)
- strtok函数的第一个参数不为 NULL ,函数将找到str中第一个标记,strtok函数将保存它在字符串中的位置。
- strtok函数的第一个参数为 NULL ,函数将在同一个字符串中被保存的位置开始,查找下一个标记。
- 如果字符串中不存在更多的标记,则返回 NULL 指针。
五、错误信息报告函数
0、错误信息报告-----strerror
char * strerror ( int errnum );
#include <stdio.h>
#include <string.h>
int main()
{
printf("%s\n", strerror(0));//No error
printf("%s\n", strerror(1));//Operation not permitted
printf("%s\n", strerror(2));//No such file or directory
printf("%s\n", strerror(3));//No such process
printf("%s\n", strerror(4));//Interrupted function call
printf("%s\n", strerror(5));//Input/output error
return 0;
}
- 返回错误码,所对应的错误信息。
六、内存操作函数
0、内存函数拷贝-----memcpy
void *memcpy( void *dest, const void src, size_t count );
#include <stdio.h>
#include <string.h>
int main()
{
int i = 0;
int a[10] = { 1,2,3,4,5,6,7,8,9,10 };
int b[10] = { 0 };
memcpy(b, a, 40);
for (i = 0; i < 10; i++)
{
printf("%d ", b[i]);
}
return 0;
}
- 函数memcpy从source的位置开始向后复制num个字节的数据到destination的内存位置。
- 这个函数在遇到 ‘\0’ 的时候并不会停下来。
- 如果source和destination有任何的重叠,复制的结果都是未定义的。
模拟实现、
#include <stdio.h>
#include <string.h>
#include <assert.h>
void* my_memcpy(void* dest, void* src, size_t num)
{
assert(dest && src);
void* tmp = dest;
while (num--)
{
(*(char*)dest) = (*(char*)src);
(char*)dest = (char*)dest + 1;
(char*)src = (char*)src + 1;
}
return tmp;
}
int main()
{
int i = 0;
int a[10] = { 1,2,3,4,5,6,7,8,9,10 };
int b[10] = { 0 };
my_memcpy(b, a, 40);
for (i = 0; i < 10; i++)
{
printf("%d ", b[i]);
}
return 0;
}
1、内存函数移动-----memmove
void *memmove( void *dest, const void src, size_t count );
- 和memcpy的差别就是memmove函数处理的源内存块和目标内存块是可以重叠的。
- 如果源空间和目标空间出现重叠,就得使用memmove函数处理。
模拟实现、
#include <stdio.h>
#include <string.h>
#include <assert.h>
void* my_memmove(void* dest, void* src, size_t num)
{
assert(dest && src);
if (src < dest)
{
while (num--)
{
(*((char*)dest + num)) = (*((char*)src + num));
}
}
else
{
while (num--)
{
*(char*)dest = *(char*)src;
((char*)dest)++;
((char*)src)++;
}
}
}
int main()
{
int a[10] = { 1,2,3,4,5,6,7,8,9,10 };
int i = 0;
my_memmove(a+2, a, 20);
for (i = 0; i < 10; i++)
{
printf("%d ", a[i]);
}
return 0;
}
2、内存函数比较-----memcmp
int memcmp( const void *buf1, const void *buf2, size_t count );
#include <stdio.h>
#include <assert.h>
#include <string.h>
int main()
{
int a[5] = { 1,2,3,4,5 };
int b[5] = { 1,2,3 };
int len=memcmp(a, b, 12);
printf("len=%d", len);
return 0;
}
- 比较从ptr1和ptr2指针开始的num个字节
模拟实现、
#include <stdio.h>
#include <assert.h>
#include <string.h>
int my_memcmp(void* buf1, void* buf2, size_t num)
{
assert(buf1 && buf2);
while (num--)
{
if (*(char*)buf1 == *(char*)buf2)
{
if (num == 0)
return 0;
((char*)buf1)++;
((char*)buf2)++;
}
if (*(char*)buf1 < *(char*)buf2)
return -1;
if (*(char*)buf1 > *(char*)buf2)
return 1;
}
}
int main()
{
int a[5] = { 1,2,3,4,5 };
int b[5] = { 1,2,3 };
int len=my_memcmp(a, b, 12);
printf("len=%d", len);
return 0;//len=0
}