本篇文章用于模拟实现
“string.h” 中的部分函数.
介绍
1.首先是一个计算字符串长度函数
2.长度不受限的字符串
3.长度受限的字符串
- strncpy()
- strncat()
- strncmp()
4.最后是一个字符串匹配函数的暴力解决方式
1.计算字符串长度函数
1.1 strlen()
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <assert.h>
size_t my_strlen(const char* str)
{
assert(str);
const char* start = str;
const char* end = str;
while (*end != '\0')
{
end++;
}
return end - start;
}
int main()
{
char arr[] = "abc\0def";
int a = my_strlen(arr);
printf("%d\n",a);
return 0;
}
2.长度不受限的字符串
2.1 strcpy()
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <assert.h>
char* my_strcpy(char* dest,const char* src)
{
assert(dest);
assert(src);
char* ret = dest;
while(*dest++ = *src++)
{
;
}
return ret;
}
int main()
{
char arr[10] = "*********";
const char* p = "abcdef";
my_strcpy(arr,p);
printf("%s\n", arr);
return 0;
}
2.2 strcat()
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <assert.h>
char* my_strcat(char* dest, const char* src)
{
assert(dest);
assert(src);
char* ret = dest;
while (*dest!='\0')
{
dest++;
;
}
while (*dest++ = *src++)
{
;
}
return ret;
}
int main()
{
char arr[20] = "hello";
char arr2[] = "world";
my_strcat(arr,arr2);
printf("%s\n",arr);
return 0;
}
2.3 strcmp()
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <assert.h>
int my_strcmp(const char* str1, const char* str2)
{
assert(str1&&str2);
while (*str1==*str2)
{
if(*str1=='\0')
{
return 0;
}
str1++;
str2++;
}
return *str1 - *str2;
}
int main()
{
char str1[] = "hello";
char str2[] = "hello";
int a = my_strcmp(str1, str2);
printf("%d\n", a);
return 0;
}
3.长度受限的字符串
3.1 strncpy()
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <assert.h>
char* my_strncpy(char* dest,const char* src,size_t count)
{
char* ret = dest;
while (count && (*dest++ = *src++) != '\0')
{
count--;
}
if (count)
{
while (--count)
{
*dest++ = '\0';
}
}
return ret;
}
int main()
{
char arr[10] = "*********";
const char* p = "abcdef";
my_strncpy(arr, p,8);
printf("%s\n", arr);
return 0;
}
3.2 strncat()
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <assert.h>
char* my_strncat(char* dest, const char* src,size_t num)
{
assert(dest);
assert(src);
char* ret = dest;
while (*dest != '\0')
{
dest++;
;
}
while (num--)
{
*dest++ = *src++;
;
}
*dest = '\0';
return ret;
}
int main()
{
char arr[20] = "hello";
char arr2[] = "world";
my_strncat(arr, arr2,3);
printf("%s\n", arr);
return 0;
}
3.3 strncmp()
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <assert.h>
int my_strncmp(const char* str1, const char* str2, size_t count)
{
assert(str1 && str2);
while (--count && *str1 == *str2)
{
if (*str1 == '\0')
{
return 0;
}
str1++;
str2++;
}
if (count == 0)
{
return 0;
}
return *str1 - *str2;
}
int main()
{
char str1[] = "hello";
char str2[] = "helo";
int a = my_strncmp(str1, str2,3);
printf("%d\n", a);
return 0;
}
4.长度受限的字符串
4.1 strstr()
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <assert.h>
char* my_strstr(const char* str1, const char* str2)
{
const char* s1 = str1;
const char* s2 = str2;
const char* p = str1;
while (*p)
{
s1 = p;
s2 = str2;
while (*s1 != '\0' && *s2 != '\0' && *s1 == *s2)
{
s1++;
s2++;
}
if (*s2 == '\0')
{
return (char*)p;
}
p++;
}
return NULL;
}
int main()
{
char arr[] = "abcdef";
char arr2[] = "bcd";
char* p = my_strstr(arr,arr2);
printf("%s\n",p);
return 0;
}
|