content
strlen function (to find the length of a string)
strcat function (string append)
strcmp function (string comparison)
strstr function (find the first occurrence of a substring in a long string)
strncpy function (copy n characters)
strncat function (append n characters)
strlen function (to find the length of a string)
counter method
#include <stdio.h>
#include <assert.h>
int my_strlen(const char* str)
{
assert(str);
int count = 0;
while (*str++)
{
count++;
}
return count;
}
recursive method
#include <stdio.h>
#include <assert.h>
int my_strlen(const char* str)
{
assert(str);
if (*str)
{
return 1 + my_strlen(str + 1);
}
else
{
return 0;
}
}
pointer-pointer method
#include <stdio.h>
#include <assert.h>
int my_strlen(const char* str)
{
assert(str);
char* tmp = str;
while (*++tmp)
{
;
}
return tmp - str;
}
Note: The subtraction of two pointers is the number of elements in the middle of the two pointers.
strcpy function (string copy)
#include <stdio.h>
#include <assert.h>
//strcpy函数返回的是目标空间的起始地址
//strcpy函数的返回类型的设置是为了实现链式访问
char* my_strcpy(char* dest, const char* str)
{
assert(dest && str);
char* ret = dest;
//记录起始地址
while (*dest++ = *str++)
{
;
}
return ret;
}
int main()
{
char arr1[20] = { 0 };
char arr2[] = "hello world";
printf("%s\n", my_strcpy(arr1, arr2));
return 0;
}
strcat function (string append)
#include <stdio.h>
#include <assert.h>
char* my_strcat(char* dest, const char* str)
{
assert(dest && str);
char* ret = dest;//记录起始地址
//找目标字符串中的'\0'
while (*dest)
{
dest++;
}
//拷贝
while (*dest++ = *str++)
{
;
}
return ret;
}
int main()
{
char arr1[20] = "hello ";
char arr2[] = "world";
printf("%s\n", my_strcat(arr1, arr2));
return 0;
}
strcmp function (string comparison)
#include <stdio.h>
#include <assert.h>
int my_strcmp(const char* dest, const char* str)
{
assert(dest && str);
while (*dest == *str)
{
if (*dest == '\0')
{
return 0;//相等
}
dest++;
str++;
}
//不相等
return *dest - *str;
}
int main()
{
char arr1[] = "abcde";
char arr2[] = "qhavs";
int ret = my_strcmp(arr1, arr2);
if (ret > 0)
{
printf(">\n");
}
else if (ret == 0)
{
printf("=\n");
}
else
{
printf("<\n");
}
return 0;
}
strstr function (find the first occurrence of a substring in a long string)
#include <stdio.h>
#include <assert.h>
char* my_strstr(const char* dest, const char* str)
{
assert(dest && str);
const char* s1 = dest;
const char* s2 = str;
const char* cur = dest;
while (*cur)
{
s1 = cur;
s2 = str;
while (*s1 && *s2 && *s1 == *s2)
{
s1++;
s2++;
}
if (*s2 == '\0')
{
return (char*)cur;
}
cur++;
}
return NULL;
}
int main()
{
char arr1[] = "abheabcdg";
char arr2[] = "bcd";
char* ret = my_strstr(arr1, arr2);
printf("%s\n", ret);
return 0;
}
strncpy function (copy n characters)
#include <stdio.h>
#include <assert.h>
char* my_strncpy(char* dest, const char* str, int ret)
{
assert(dest && str);
char* tmp = dest;
int i = 0;
for (i = 0; i < ret; i++)
{
*dest++ = *str++;
}
return tmp;
}
int main()
{
char arr1[10] = "helloxxxx";
char arr2[7] = "world";
printf("%s\n", my_strncpy(arr1, arr2, 5));
return 0;
}
strncat function (append n characters)
#include <stdio.h>
#include <assert.h>
char* my_strncat(char* dest, const char* str, int n)
{
assert(dest && str);
char* ret = dest;//记录起始地址
//找目标字符串中的'\0'
while (*dest)
{
dest++;
}
//拷贝
int i = 0;
for (i = 0; i < n; i++)
{
*dest++ = *str++;
}
//字符串追加最后要多加一个'\0'
*dest = '\0';
return ret;
}
int main()
{
char arr1[20] = "hello\0xxxxxxxx";
char arr2[7] = "world";
printf("%s\n", my_strncat(arr1, arr2, 5));
return 0;
}