- strlen
- strncpy
- strcmp
- strcat
- strstr
- memcpy
- memmove
strlen
strlen函数的作用就是求字符串的首元素开始往后计算字符串的长度,直到’\0‘,但不会把\0的长度计算进去
#include<stdio.h>
size_t Strlen(const char* src)
{
size_t count = 0;
while (*src != '\0')
{
src++;
count++;
}
return count;
}
int main()
{
char arr[] = "abcdef";
printf("%zd", Strlen(arr));
return 0;
}
strncpy
strncpy的作用就是从source指向的字符串复制num个字节的内容到destination指向的字符串去
如果source的长度小于num,则填充0
注意source和destination的内容在空间上不要重叠,如要进行重叠空间内容的拷贝,则参考后续的memmove
#include<stdio.h>
size_t Strlen(const char* src)
{
size_t count = 0;
while (*src != '\0')
{
src++;
count++;
}
return count;
}
char* Strncpy(char* dest, const char* src, size_t num)
{
char* ret = dest;
size_t len = Strlen(src);
if (len >= num)
{
while (num--)
{
*dest++ = *src++;
}
}
else
{
int tmp = num - len;
while (len--)
{
*dest++ = *src++;
}
while (--tmp > 0)
{
*dest++ = '\0';
}
}
*dest = '\0';
return ret;
}
strcmp
strcmp函数的作用是比较两个字符串,str1小于str2返回小于0的数字,相等返回0.大于则返回大于0的数字
vs中一般是返回1、0、-1
#include<stdio.h>
#include<assert.h>
//c++可以用函数重载把strcmp和strncmp实现在一起
int Strcmp(const char* arr1,const char* arr2)
{
assert(arr1 && arr2);
while (*arr1 != '\0' && *arr2 != '\0' && *arr1 == *arr2)
{
arr1++;
arr2++;
}
if (*arr1 != '\0' && *arr2 != '\0' && *arr1 > *arr2)
{
return 1;
}
else if (*arr1 != '\0' && *arr2 != '\0' && *arr1 < *arr2)
{
return -1;
}
else if (*arr1 == '\0' && *arr2 == '\0')
{
return 0;
}
else
{
if (*arr1 != '\0' && *arr2 == '\0')
{
return 1;
}
else
{
return -1;
}
}
// assert(str1 && str2);
//
// while (*str1 == *str2)
// {
// if (*str1 == '\0')
// return 0;
//
// str1++;
// str2++;
// }
//
// return (*str1 - *str2);
}
int main()
{
char arr1[] = "abcdef";
char arr2[] = "abc";
char arr3[] = "abcdefg";
char arr4[] = "abd";
char arr5[] = "abb";
printf("%d\n", Strcmp(arr1, arr2));
printf("%d\n", Strcmp(arr1, arr3));
printf("%d\n", Strcmp(arr1, arr3));
printf("%d\n", Strcmp(arr1, arr4));
printf("%d\n", Strcmp(arr1, arr5));
return 0;
}
strcat
strcat函数的作用就是在destination指向的字符串后面追加上sourse指向的字符串
source和destination指向的字符串也不能重叠
#include<stdio.h>
#include<assert.h>
char* Strcat(char* arr1,const char* arr2)
{
assert(arr1 && arr2);
char* ret = arr1;
while (*arr1 != '\0')
{
arr1++;
}
while (*arr1++ = *arr2++)
{
;
}
return ret;
}
int main()
{
//如果这里我不提前给arr1一个20的初始空间的话
//编译器自动赋值成6,追加之后arr1长度会超出范围
char arr1[20] = "abcdef";
char arr2[] = "ghi";
printf("%s", Strcat(arr1, arr2));
return 0;
}
strstr
strstr函数的作用就是在str1指向的字符串中找到str2指向的字符串第一次在str1指向的字符串中出现的位置
#include<stdio.h>
#include<assert.h>
size_t Strlen(const char* src)
{
size_t count = 0;
while (*src != '\0')
{
src++;
count++;
}
return count;
}
char* strstr(const char* arr1, const char* arr2)
{
assert(arr1 && arr2);
char* ret = arr1;
char* p1 = ret;
char* p2 = arr2;
size_t len1 = Strlen(arr1);
size_t len2 = Strlen(arr2);
while (*(ret + len2) != '\0')
{
if (*ret != *p2)
{
ret++;
len1--;
}
if (len1 > 0)
{
p1 = ret;
}
else
{
return NULL;
}
while (*p1 == *p2)
{
p1++;
p2++;
}
if (*p2 == '\0')
{
return ret;
}
ret++;
p1 = ret;
p2 = arr2;
}
}
int main()
{
char arr1[] = "abbbcbcf";
char arr2[] = "bbc";
char arr3[] = "bbbb";
char arr4[] = "xyz";
printf("%s\n", strstr(arr1, arr2));
printf("%s\n", strstr(arr1, arr3));
printf("%s\n", strstr(arr1, arr4));
return 0;
}
memcpy
memcpy是内存块拷贝,是更精确的拷贝函数,并且可以针对各种类型的变量(strcpy只针对字符串)
source和destination指向的空间不应该重叠(重叠的考虑memmove)
#include<stdio.h>
#include<assert.h>
void* Memcpy(void* arr1,const void* arr2, int num)
{
void* ret = arr1;
assert(arr1 && arr2);
while (num--)
{
*((char*)arr1+num) = *((char*)arr2 + num);
}
}
int main()
{
int arr1[20] = { 0 };
int arr2[] = { 1,2,3,4,5,6,7,8,9 };
Memcpy(arr1, arr2, 4 * sizeof(arr2[0]));
return 0;
}
memmove
加强版memcpy,可以拷贝空间上重叠的内容
#include<stdio.h>
#include<assert.h>
void* Memmove(void* arr1,const void* arr2, int num)
{
void* ret = arr1;
assert(arr1 && arr2);
if (arr1 < arr2)
{
while (num--)
{
*((char*)arr1) = *((char*)arr2);
++(char*)arr1;
++(char*)arr2;
}
}
else
{
while (num--)
{
*((char*)arr1 + num) = *((char*)arr2 + num);
}
}
}
int main()
{
int arr1[] = { 1,2,3,4,5,6,7,8,9 };
Memmove(arr1+4, arr1, 4 * sizeof(arr1[0]));
return 0;
}