本内容是重点介绍使用字符函数、字符串函数、内存函数的库函数的使用和注意事项。
求字符串长度 字符串查找
strlen strstr
长度不收限制的字符串函数 错误信息报告
strcpy strerror
strcat 内存操作函数
strcmp memcpy
长度受限制的字符串函数介绍 memmove
strncpy memset
strncat memcmp
strncmp 字符操作
:求字符串长度
size_t strlen (const char * str);
#include
#include
int main()
{
char arr1[]= "abcdef";
int ret = strlen(arr1);
printf("%d\n", ret);
return 0;
}
#include
size_t my_strlen(const char* str)
{
char* st = str;
while (*st++);
return st-str-1;
}
int main()
{
char arr1[] = "abcdef";
int ret = my_strlen(arr1);
printf("%d\n", ret);
}
: 字符串拷贝
char* strcpy(char * destination, const char * source)
#include
#include
int main()
{
char arr1[20] = { 0 };
char arr2[] = "abcdef";
printf("%s\n",strcpy(arr1, arr2));
return 0;
}
#include
#include
char* my_strcpy(char* dest, const char* src)
{
assert(dest && src);
char* ret = dest;
while (*dest++ = *src++)
{
;
}
return ret;
}
int main()
{
char arr1[20] = { 0 };
char arr2[10] = "abcdef";
printf("%s\n", my_strcpy(arr1,arr2));
return 0;
}
:字符串连接(字符串追加)
char * strcat ( char * destination, const char * source)
#include
#include
int main()
{
char a1[20] = "abc";
char a2[] = "def";
printf("%s\n", strcat(a1, a2));
return 0;
}
#include
char* my_strcat(char* dest, const char* src)
{
char* ret = dest;
while (*dest)
{
dest++;
}
while (*dest++ = *src++)
{
;
}
return ret;
}
int main()
{
char a1[20] = "abc";
char a2[] = "def";
printf("%s\n", my_strcat(a1, a2));
}
:字符串比较,比较的是字符串的内容,不是长度
int strcmp ( const char * str1,const char * str2);
#include
#include
int main()
{
char a1[] = "abcd";
char a2[] = "abcd";
int ret = strcmp(a1, a2);
printf("%d\n", ret);
return 0;
}
#include
int my_strcmp(const char* s1, const char* s2)
{
while (*s1 == *s2)
{
if (*s1 == '\0')
{
return 0;
}
s1++;
s2++;
}
return *s1 - *s2;
}
int main()
{
char a1[] = "abcd";
char a2[] = "abcd";
int ret = my_strcmp(a1, a2);
printf("%d\n", ret);
return 0;
}
char * strncpy ( char * destination , const char * source , size_t num);
#include
#include
int main()
{
char arr1[20] = { 0 };
char arr2[] = "abcdef";
printf("%s\n", strncpy(arr1, arr2 + 2, 2));//输出结果cd
return 0;
}
char * strncat ( char * destination , const char * source ,size_t num);
#include
#include
int main()
{
char str1[20];
char str2[20];
strcpy(str1, "To be ");
strcpy(str2, "or not to be");
strncat(str1, str2, 6);
puts(str1);//输出结果,To be or not
return 0;
}
int strncmp ( const char * str1, const char * str2, size_t num );
比较到出现另个字符不一样或者一个字符串结束或者num个字符全部比较完。
#include
#include
int main()
{
char str[][5] = { "R2D2","C3PO ","R2A6" };
int n;
puts("Looking for R2 astromech droids...");
for (n = 0; n < 3; n++)
if (strncmp(str[n], "R2xx", 2) == 0)
{
printf("found %s\n", str[n]);//输出结果 R2D2 R2A6
}
return 0;
}
:字符串查找函数 在一个字符串中查找子字符串
char * strstr ( const char * str2, const char * str1);
#include
#include
int main()
{
char arr1[] = "i am a good student,hehe student";
char arr2[] = "student";
//查找arr1中arr2第一次出现的位置
char* ret = strstr(arr1, arr2);
if (ret == NULL)
{
printf("找不到\n");
}
else
{
printf("%s\n", ret);//输出 student,hehe student
}
return 0;
}
#include
#include
char* my_strstr(const char* str1, const char* str2)
{
assert(str1 && str2);
char* s1;
char* s2;
char* cp = str1;
if (*str2 == '\0')
{
return str1;
}
while (*cp)
{
s1 = cp;
s2 = str2;
while (*s1 != '\0' && *s2 != '\0' && *s1 == *s2)
{
s1++;
s2++;
}
if (*s2 == '\0')
{
return cp;
}
cp++;
}
return NULL;
}
int main()
{
char a1[] = "hello my name is wz zz";
char a2[] = "wz";
char* ret = my_strstr(a1, a2);
if (ret == NULL)
{
printf("找不到了\n");
}
else
{
printf("%s\n", ret);
}
return 0;
}
:分割字符串
char * strtok ( char * str, const char * sep );
#include
#include
int main()
{
char arr1[] = "[email protected]";
char arr2[100] = { 0 };//临时数据
//zpw\0bitedu.tech\0
char sep[] = "@.";
strcpy(arr2, arr1);
char* ret = NULL;
//分割字符串
for (ret=strtok(arr2, sep); ret!=NULL; ret=strtok(NULL, sep))
{
printf("%s\n", ret);
}
//strtok(arr2, sep);
//strtok(NULL, sep);
return 0;
}
char * strerror(int errnum);
返回误码,所对应的错误信息 。
#include
#include
int main()
{
printf("%s\n", strerror(0));
printf("%s\n", strerror(1));
printf("%s\n", strerror(2));
printf("%s\n", strerror(3));
return 0;
}
:拷贝的整形数据
void * memcpy ( void * destination, const void * source, size_t num );
#include
#include
struct {
char name[40];
int age;
} person, person_copy;
int main()
{
char myname[] = "Pierre de Fermat";
memcpy(person.name, myname, strlen(myname) + 1);
person.age = 46;
memcpy(&person_copy, &person, sizeof(person));
printf("person_copy: %s, %d \n", person_copy.name, person_copy.age);
return 0;
}
#include
#include
void* my_memcpy(void* dest, const void* src, size_t count)
{
void* ret = dest;
assert(dest && src);
while (count--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest+1;
src = (char*)src+1;
}
return ret;
}
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
int i = 0;
my_memcpy(arr + 2, arr, 16);
for (i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
:内存拷贝时,出现内存重复的现象
void * memmove ( void * destination, const void * source, size_t num );
#include
#include
int main ()
{
char str[] = "memmove can be very useful. ........... ";
memmove (str+20,str+15,11);
puts (str);
return 0;
}
#include
#include
void* my_memmove(void* dest, const void* src, size_t count)
{
void* ret = dest;
assert(dest && src);
if (dest < src)
{
//dest在src左边时,从前往后拷贝
while (count--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
return ret;
}
else
{
//dest在src右边时,从后往前拷贝
while (count--)
{
*((char*)dest+count) = *((char*)src+count);
}
return ret;
}
}
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
int i = 0;
my_memmove(arr + 2, arr, 16);
for (i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
int memcmp ( const void * ptr1, const void * ptr2, size_t num );
#include
#include
int main()
{
int arr1[] = { 1,2,3,4,5 };
int arr2[] = { 1,2,3,6,6 };
int ret = memcmp(arr1, arr2, 12);
printf("%d\n", ret);//输出结果为 0
return 0;
}
void * memset(void *dest,int c,size_t count);
#include
#include
int main()
{
int arr[] = { 1,2,3,4,5,6 };
memset(arr, 1, 20);
return 0;
}