长度不受限制的字符串函数:strcpy,strcat,strcmp
长度受限制的字符串函数:strncpy,strncat,strncmp
strcpy函数模拟实现
#include<stdio.h>
#include<string.h>
#include<assert.h>
char* my_strcpy(char* dest,const char* src)
{
assert(dest != NULL);
assert(src != NULL);
int ret = dest;
while (*dest++ = *src++)
{
;
}
return ret;
}
int main()
{
char arr1[] = "abcdefghi";
char arr2[] = "bit";
my_strcpy(arr1, arr2);
printf("%s\n", arr1);
return 0;
}
strcat函数模拟实现
#include<stdio.h>
#include<assert.h>
void my_strcat(char* dest, const char* src)
{
assert(dest && src);
char* len = strlen(dest)+ dest;
while (*len++ = *src++)
{
;
}
}
int main()
{
char arr1[] = "hello";
char arr2[] = "world";
my_strcat(arr1, arr2);
printf("%s\n", arr1);
return 0;
}
#include<stdio.h>
#include<assert.h>
char* my_strcat(char* dest, const char* src)
{
char* ret = dest;
assert(dest && src);
//1.找到目的字符串'\0'
while (*dest != '\0')
{
dest++;
}
while (*dest++ = *src++)
{
;
}
return ret;
}
int main()
{
char arr1[] = "hello";
char arr2[] = "world";
char* i = my_strcat(arr1, arr2);
printf("%s\n",i);
return 0;
}
strcmp
//strcmp函数的模拟实现
//string 1>string 2 return >0
//string 1<string 2 return <0
//string 1=string 2 return 0
#include<stdio.h>
#include<assert.h>
int my_strcmp(const char* str1,const char* str2)
{
assert(str1 && str2);
//比较
while (*str1!='\0'&&*str2!='\0')
{
if (*str1 == *str2)
{
str1++;
str2++;
}
else if (*str1 > *str2)
{
return 1;
}
else
{
return -1;
}
}
return 0;
}
int main()
{
char* p1 = "abcdef";
char* p2 = "abcasd";
int ret = my_strcmp(p1, p2);
printf("%d", ret);
return 0;
}
#include<stdio.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++;
}
if (*str1 > *str2)
return 1;
else
return -1;
//return (*str1 - *str2);
}
int main()
{
char* p1 = "abcdef";
char* p2 = "abcdef";
int ret = my_strcmp(p1, p2);
printf("%d", ret);
return 0;
}
strstr函数的模拟实现
#include<stdio.h>
#include<assert.h>
char* my_strstr(const char* p1, const char* p2)
{
assert(p1 != NULL);
assert(p2 != NULL);
if (*p2 == '\0')
{
return p1;
}
char* p3 = p2;
int count = 0;//记录连续相等字符的长度
while (*p1)
{
if (*p1 == *p2)//遇到字符相等时,两个指针同时进一位
{
p1++;
p2++;
count++;
}
else//字符串不相等
{
p1++;//p1继续向前
p2 = p3;//p2还原到开始位置
count = 0;//此时两个字符不相等而不连续,所以归零
}
if (*p2 == '\0')
return p1-count;
}
}
int main()
{
char* p1 = "abdcdefghi";
char* p2 = "def";
char*ret = my_strstr(p1, p2);
if (ret == NULL)
{
printf("子串不存在\n");
}
else
{
printf("子串存在:%s\n", ret);
}
return 0;
}
此法当 p1为"abdcddefghi" 也就时存在两个连续的d 时行不通
库函数
#include<stdio.h>
#include<assert.h>
char* my_strstr(const char* p1, const char* p2)
{
assert(p1 != NULL);
assert(p2 != NULL);
if (*p2 == '\0')
{
return (char*)p1;
}
char* s1 = p1;
char* s2 = p2;
char* cur = p1;
while (*cur)
{
s1 = cur;
s2 = p2;
while ((*s2!='\0') && (*s1 != '\0') && (*s1 == *s2))//while(*s1 && *s2 && !(s1-*s2))
{
s1++;
s2++;
}
if (!*s2)//if(*s2=='\0')
{
return cur; //找到子串
}
cur++;
}
return NULL;//找不到空指针
}
int main()
{
char* p1 = "abcddefghi";
char* p2 = "def";
char*ret = my_strstr(p1, p2);
if (ret == NULL)
{
printf("子串不存在\n");
}
else
{
printf("子串存在:%s\n", ret);
}
return 0;
}