1.模拟strcpy(字符串拷贝函数)
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char *my_strcpy(char *dest,const char *src)
{
char *pc=dest;
assert(src);
assert(dest);
while(*dest++ = *src++)
{
;
}
return pc;
}
int main()
{
char *pa="abcdef";
char pb[10]={0};
my_strcpy(pb,pa);
"%s\n",pb);
"pause");
return 0;
}
strncpy:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char *my_strncpy(char *dest,const char *src,size_t n)
{
char *ret=dest;
assert(src);
assert(dest);
while(n--)
{
*ret++=*src++;
}
'\0';//拷贝'\0'
return dest;
}
int main()
{
int num = 0;
char pa[]="abcdefgh";
char pb[20]={0};
char *ret=NULL;
"请输入你要拷贝字符的个数:");
"%d",&num);
ret=my_strncpy(pb,pa,num);
"%s\n",ret);
"pause");
return 0;
}
2.模拟strlen(求取字符串长度)
//普通实现
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
int my_strlen(const char *src)
{
int count=0;
assert(src);
while(*src++)
{
count++;
}
return count;
}
int main()
{
int ret=0;
char *pa="abcdefgh";
ret=my_strlen(pa);
"所求字符串的长度为:%d\n",ret);
"pause");
return 0;
}
//指针实现
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
int my_strlen(const char *str)
{
char *tmp=str;
while(*tmp++)
{
;
}
return tmp-str-1;
}
int main()
{
char *pa="abcdefgh";
int ret = my_strlen(pa);
"%d\n",ret);
"pause");
return 0;
}
//递归实现
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
int my_strlen(const char *str)
{
if(!*str)
return 0;
else
return 1+my_strlen(str+1);
}
int main()
{
char *pa="abcdefgh";
int ret = my_strlen(pa);
"%d\n",ret);
"pause");
return 0;
}
3.模拟strstr(在所给字符串中查找子串第一次出现的位置)
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char *my_strstr(const char *str,const char *dest)
{
char *tmp=NULL;
const char *string1=str;
const char *string2=dest;
assert(str);
assert(dest);
if(!*string2)
{
return (char *)string1;
}
while(*string1)
{
char *)string1;
while(*string1 && *string2 && *string1==*string2)
{
string1++;
string2++;
}
if(!*string2)
{
return (char *)tmp;
}
string1++;
}
return NULL;
}
int main()
{
int result=0;
char *pa="abcdef";
char *pb="cde";
char *ret=my_strstr(pa,pb);
result=ret-pa+1;
if(NULL != ret)
{
"%s found at position %d\n",pb,result);
}
else
{
"%s not found\n",pb);
}
"pause");
return 0;
}
strrstr:库中没有该函数,仿照strchrr实现(在所给字符串中查找子串最后一次出现的位置
<span style="font-size:14px;">#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char *my_strstr(const char *dest,const char *src)
{
const char *tmp = NULL;
assert(dest);
assert(src);
while(*dest && *src)
{
tmp=src;
while(*src && *dest==*src)
{
src++;
dest++;
}
if(!*dest)
{
return (char*)tmp;
}
src++;
}
return NULL;
}
int main()
{
char *pa = "adcbdcdcbef";
char *pb = "dcb";
char *ret = my_strstr(pb,pa);
if(ret!=NULL)
{
"%s\n",ret);
}
else
{
"没找到该子串\n");
}
"pause");
return 0;
}</span><span style="font-size: 24px;">
</span>
<span style="font-size:14px;">#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char *my_strrstr(const char *dest,const char *src)
{
const char *psrc = NULL;
const char *pdest = dest;
const char *tmp = NULL;
assert(dest);
assert(src);
while(*src)
{
psrc=src;
while(*src && *dest==*src)
{
src++;
dest++;
}
if(*dest == '\0')
{
tmp=psrc;
}
src=psrc;
dest=pdest;
src++;
}
if(tmp!=NULL)
{
return (char *)tmp;
}
return NULL;
}
int main()
{
char *pa = "adcbdcb";
char *pb = "dcb";
char *ret = my_strrstr(pb,pa);
if(ret!=NULL)
{
"%s\n",ret);
}
else
{
"没找到该子串\n");
}
"pause");
return 0;
}</span>
4.strcat(字符串连接)
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char *my_strcat(char *dest,const char *src)
{
char *tmp=dest;
assert(dest);
if(!*src)
{
return tmp;
}
while(*tmp)
{
tmp++;
}
while(*tmp++ = *src++)
{
;
}
return dest;
}
int main()
{
char *pa="efgh";
char pb[20]="abcd";
my_strcat(pb,pa);
"%s\n",pb);
"pause");
return 0;
}
<span style="font-size:14px;">#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char *my_strstr(const char *dest,const char *src)
{
const char *tmp = NULL;
assert(dest);
assert(src);
while(*dest && *src)
{
tmp=src;
while(*src && *dest==*src)
{
src++;
dest++;
}
if(!*dest)
{
return (char*)tmp;
}
src++;
}
return NULL;
}
char *my_strrstr(const char *dest,const char *src)
{
const char *tmp = NULL;
const char *psrc=NULL;
const char *pdest=dest;
assert(dest);
assert(src);
while(*src)
{
tmp = my_strstr(pdest,src);
if(tmp!=NULL)
{
psrc=tmp;
src=tmp;
}
src++;
}
if(psrc!=NULL)
{
return (char *)psrc;
}
return NULL;
}
int main()
{
char *pa = "adcbdcb";
char *pb = "dcb";
char *ret = my_strrstr(pb,pa);
if(ret!=NULL)
{
"%s\n",ret);
}
else
{
"没找到该子串\n");
}
"pause");
return 0;
}
</span>
strncat:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char *my_strncat(char *dest,const char *src,size_t n)
{
char *ret=dest;
assert(src);
assert(dest);
while(*ret)
{
ret++;
}
while(n--)
{
*ret++=*src++;
}
'\0';//追加'\0'
return dest;
}
int main()
{
int num = 0;
char pa[20]="abcd";
char pb[]="efgghhh";
char *ret=NULL;
"请输入你要追加字符的个数:");
"%d",&num);
ret=my_strncat(pa,pb,num);
"%s\n",ret);
"pause");
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
char *my_strrstr(const char *dest,const char *src)//从后向前查找
{
int len1=strlen(src);
int len2=strlen(dest);
const char*psrc=src;
assert(dest);
assert(src);
while(src+len1-1>src)
{
while(*psrc)//先找到最后位置
{
psrc++;
}
while(*dest)//先找到最后位置
{
dest++;
}
while(--len1 && --len2 && *dest==*psrc)
{
dest--;
psrc--;
}
if(len2==0)
return (char*)psrc-1;
}
return NULL;
}
int main()
{
char *pa = "adcbdcb";
char *pb = "dcb";
char *ret = my_strrstr(pb,pa);
if(ret!=NULL)
{
"%s\n",ret);
}
else
{
"没找到该子串\n");
}
"pause");
return 0;
}
5.strchr(在字符串中查找某个字符首次出现的位置)
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char *my_strchr(const char *src,int ch)
{
char *tmp=(char *)src;
assert(src);
while(*tmp++)
{
src=tmp;
if(*tmp == ch)
{
return (char *)src;
}
}
return NULL;
}
int main()
{
int ch='c';
char *pa="abcdef";
char *ret=my_strchr(pa,ch);
int result=ret-pa+1;
if(ret != NULL)
{
"Result:\tfirst %c found at position %d\n\n", ch, result);
}
else
{
"Result:\t%c not found\n",ch);
}
"pause");
return 0;
}
strrchr(在字符串中查找某个字符最后一次出现的位置)
<span style="font-size:18px;"><span style="font-size: 24px;">//从后向前找</span>
</span>
<span style="font-size:14px;">#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char *my_strrchr(const char *str,int ch)
{
const char *tmp = str;
assert(str);
while(*str++)
{
;
}
while(--str>tmp)
{
if(*str == ch)
return (char *)str;
}
return NULL;
}
int main()
{
char *pa = "adbdcdef";
char *ret = my_strrchr(pa,'d');
if(ret!=NULL)
{
"%s\n",ret);
}
else
{
"没找到该字符\n");
}
"pause");
return 0;
}</span>
//每次找到指定字符,先保存起来,最后返回该字符所在地址
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char *my_strrchr(const char *str,int ch)
{
const char *tmp = NULL;
assert(str);
while(*str)
{
if(*str==ch)
tmp=str;
str++;
}
if(tmp!=NULL)
return (char*)tmp;
return NULL;
}
int main()
{
char *pa = "adcbdcdef";
char *ret = my_strrchr(pa,'g');
if(ret!=NULL)
{
"%s\n",ret);
}
else
{
"没找到该字符\n");
}
"pause");
return 0;
}
//通过strchr实现
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char *my_strchr(const char *str,int ch)
{
assert(str);
while(*str)
{
if(*str == ch)
return (char*)str;
str++;
}
return NULL;
}
char *my_strrchr(const char *str,int ch)
{
char *tmp = NULL;
assert(str);
while(*str)
{
if(*str == ch)
tmp = my_strchr(str,ch);
str++;
}
if(tmp!=0)
return (char*)tmp;
return NULL;
}
int main()
{
char *pa = "adcbdcdef";
char *ret = my_strrchr(pa,'d');
if(ret!=NULL)
{
"%s\n",ret);
}
else
{
"没找到该字符\n");
}
"pause");
return 0;
}
6.strcmp(字符串比较)
实现1:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
int my_strcmp(const char *str1,const char *str2)
{
assert(1);
assert(2);
while(*str1 && *str2==*str1)
{
str1++;
str2++;
}
return *str1-*str2;
}
int main()
{
char *pa="abczef";
char *pb="abcfgjdh";
int ret=my_strcmp(pa,pb);
if(ret>0)
{
"%s greater than %s\n",pa,pb);
}
else if(ret==0)
{
"%s eaqul to %s\n",pa,pb);
}
else
{
"%s less than %s\n",pa,pb);
}
"pause");
return 0;
}
实现2:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
int my_strcmp(const char *str1,const char *str2)
{
int ret = 0;
assert(1);
assert(2);
while(!(ret=*(unsigned char *)str1-*(unsigned char *)str2) && *str2)
{
++str1;
++str2;
}
if(ret<0)
{
return -1;
}
else if(ret>0)
{
return 1;
}
return ret;
}
int main()
{
char *pa="abcdef";
char *pb="abcfgjdh";
int ret=my_strcmp(pa,pb);
if(ret>0)
{
"%s greater than %s\n",pa,pb);
}
else if(ret==0)
{
"%s eaqul to %s\n",pa,pb);
}
else
{
"%s less than %s\n",pa,pb);
}
"pause");
return 0;
}
strncmp:
7.memcpy:内存拷贝函数(两块内存不关联)
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
int my_strncmp(const char *str1,const char *str2,size_t n)
{
assert(str1);
assert(str2);
while(--n)
{
*str1++ == *str2++;
}
return (*str1-*str2);
}
int main()
{
int num = 0;
int ret = 0;
char *pa="abcd";
char *pb="abcf";
"请输入你要比较字符的个数:");
"%d",&num);
ret=my_strncmp(pa,pb,num);
if(ret>0)
{
"%s greater than %s\n",pa,pb);
}
else if(ret==0)
{
"%s eaqul to %s\n",pa,pb);
}
else
{
"%s less than %s\n",pa,pb);
}
"pause");
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
void *my_memcpy(void *dest,const void *src,size_t n)
{
char *pdest=(char *)dest;
const char *psrc=(const char *)src;
assert(dest);
assert(src);
while(n--)
{
*pdest++=*psrc++;
}
return dest;
}
int main()
{
int i = 0;
int arr1[20]={0};
int arr2[]={1,2,3,4,5,6,7,8,9,0};
int sz = sizeof(arr2)/sizeof(arr2[0]);
my_memcpy(arr1,arr2,sz);
for(i=0; i<sizeof(arr2)/sizeof(arr2[0]); i++)
{
"%d ",arr2[i]);
}
"pause");
return 0;
}
memmove:(两块内存有关联)
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
void *my_memmove(void *dest,const void *src,size_t n)
{
char *pdest=(char *)dest;
const char *psrc=(char *)src;
assert(dest);
assert(src);
if(psrc+n>pdest && pdest>psrc)//从后向前拷贝
{
while(n--)
{
*(pdest+n)=*(psrc+n);
}
}
else//从前向后拷贝
{
while(n--)
{
*pdest++=*psrc++;
}
}
return dest;
}
int main()
{
int i = 0;
int num = 0;
int arr2[]={1,2,3,4,5,6,7,8,9,0};
"请输入你要拷贝内存的大小:");
"%d",&num);
sizeof(int));
for(i=0; i<sizeof(arr2)/sizeof(arr2[0]); i++)
{
"%d ",arr2[i]);
}
"\n");
"pause");
return 0;
}
标签:return,dest,ret,C语言,char,int,字符串,include,模拟
From: https://blog.51cto.com/u_16147764/6402919