#include<stdio.h>
char* strcpy(char* dest,const char* src); //字符串拷贝
char* strcat(char* dest,const char* src); //字符串连接,返回dest字符串
char* strncat(char* dest, const char* src, int size);//把src前n个字符串连接到dest,返回dest字符串
int strcmp(const char* src1,const char* src2); //字符串比较 返回值: 1:> 0:= -1<
int strncmp(const char* src,const char* dst,int size); //dst前n个字符和src比较返回值: 1:> 0:= -1<
int strlen(const char* src); //返回字符串长度
char* strchr(const char* src, int ch); //字符ch在字符串第一次出现的位置,返回出现字符ch位置开始到字符串结束位置的指针
char* strrchr(const char* src, int ch); //字符ch在字符串最后一次出现的位置,返回出现字符ch位置开始到字符串结束位置的指针
char* strstr(const char* src, const char* sub); //字符串sub在字符串第一次出现的位置,返回出现字符串sub位置开始到字符串结束位置的指针
//memery copy operate function==================//
void* memcpy(void* dest,const void* src,unsigned int size); //把src的内容拷到dest内存里去,并返回dest指向的内存地址
void* memset(void* dst, int ch, unsigned int size); //把dst内存中的size大小用使用ch来初始化,并返回dest指向的内存地址
int memcmp(const void* src1, const void* src2, unsigned int size); //比较内存中src1与src2中的前size 个字符是否相等,1:> 0:= -1< (see strncmp(const char*,const char*,unsigned int))
void* memchr(const void* dst, int ch, unsigned int size); //在内存中的某一范围内,查找特定的字符ch,并返回指向ch的指针
void* memmove(void* dst, const void* src, unsigned int size); //memmove()与memcpy()一样都是用来拷贝src所指的内存内容前n个字节到dest所指的地址上。不同的是,当src和dest所指的内存区域重叠时,memmove()仍然可以正确的处理,不过执行效率上会比使用memcpy()略慢些.返回指向dest的指针
//memery copy operate function==================//
int main()
{
// printf("the string len=%d/n",strlen("helloWorld"));
// char dest[20];
// printf("the string is=%s/n",strcpy(dest,"helloWorld"));
// char dest[30]="hello--";
// printf("after connect sub string is=%s/n",strcat(dest,"world"));
// char dest[30]="hello--";
// printf("after connect sub string is=%s/n",strncat(dest,"world_programe",4));
// printf("the string compare result is=%d/n",strcmp("hello","aello"));
// printf("the string compare result is=%d/n",strncmp("hello","hello_good",5));
// printf("the char appear position is=%s/n",strchr("hello-r-es-d",'-'));
// printf("the char appear position is=%s/n",strrchr("hel*lo-r*-e*s-d",'*'));
// printf("the sub string is=%s/n",strstr("hello_world","lo"));
char dest[20];
// printf("the sub string is=%s/n",memcpy(dest,"hello..111",sizeof(dest)));
printf("the sub string is=%s/n",memset(dest,'0',sizeof(dest)));
return 0;
}
//=========================================================//
int strlen(const char* src)
{
const char* p=src;
while(*p++!='/0');
return p-src-1;
}
//=========================================================//
char* strcpy(char* dest,const char* src)
{
if(dest&&src)
{
int i=0;
while((*(dest+i)=*(src+i))!='/0') i++;
*(dest+i)='/0';
// return dest;
}
return dest;
}
//=========================================================//
char* strcat(char* dest,const char* src)
{
if(dest&&src)
{
int len=strlen(dest);
// printf("len=%d/n",len);
int i=0;
while((*(dest+len+i)=*(src+i))!='/0') i++;
*(dest+len+i)='/0';
}
return dest;
}
//=========================================================//
char* strncat(char* dest, const char* src, int size)
{
if(dest&&src)
{
int len=strlen(dest);
int i=0;
while((i<size)&&((*(dest+len+i)=*(src+i))!='/0')) i++;
}
return dest;
}
//=========================================================//
int strcmp(const char* src1,const char* src2)
{
int equal;
int i=0;
while(!(equal=*(unsigned char*)(src1+i)-*(unsigned char*)(src2+i))&&(*(src1+i))&&(*(src2+i)))
i++;
if(equal<0) return -1;
else if(equal>0) return 1;
else return 0;
}
//=================比较两个字符串前size 个字符串是否相等==========================//
int strncmp(const char* src1,const char* src2,int size)
{
int equal;
int i=0;
while((i<size)&&!(equal=*(unsigned char*)(src1+i)-*(unsigned char*)(src2+i))&&(*(src1+i))&&(*(src2+i)))
i++;
if(equal<0) return -1;
else if(equal>0) return 1;
else return 0;
}
//=========================================================//
char* strchr(const char* src, int ch)
{
int i=0;
while(*(src+i)&&(*(src+i)!=ch)) i++;
return (char*)(src+i);
}
//=========================================================//
char* strrchr(const char* src, int ch)
{
int len=strlen(src);
int i=0;
while(i<len&&(*(src+len-i)!=ch)) i++;
return (char*)(src+(len-i));
}
//=========================================================//
char* strstr(const char* src, const char* sub)
{
if(src&&sub)
{
int subLen=strlen(sub);
int srcLen=strlen(src);
int nomatch=1;
int fds=srcLen-subLen+1;
int i=0;
if(fds>0) //find counts in the string
while((nomatch=strncmp(src+i,sub,subLen))&&fds--)//把当前src的指针往后推,直到找到与sub指针相同为止
i++;
if(nomatch)
return 0;
else
return (char*)(src+i);
}
return 0;
}
//====================memory operate=====================================//
//====================memory operate=====================================//
void* memcpy(void* dest,const void* src,unsigned int size)
{
if(dest&&src&&size>0)
{
int i=0;
unsigned char* p=(unsigned char*)dest;
unsigned char* q=(unsigned char*)src;
while((i<size)&&(*(p+i)=*(q+i)))
i++;
return dest;
}
return 0;
}
//=========================================================//
void* memset(void* dst, int ch, unsigned int size)
{
int i=0;
unsigned char* p=(unsigned char*)dst;
while((i<size)&&(*(p+i)=ch))
i++;
return dst;
}
//=========================================================//
int memcmp(const void* src1, const void* src2, unsigned int size)
{
}
//=========================================================//
void* memchr(const void* dst, int ch, unsigned int size)
{
}
void* memmove(void* dst, const void* src, unsigned int count)
{
void * ret = dst;
if (dst <= src || (char *)dst >= ((char *)src + count))
{
while (count--)
{
*(char *)dst = *(char *)src;
dst = (char *)dst + 1;
src = (char *)src + 1;
}
}
else
{
dst = (char *)dst + count - 1;
src = (char *)src + count - 1;
while (count--)
{
*(char *)dst = *(char *)src;
dst = (char *)dst - 1;
src = (char *)src - 1;
}
}
return(ret);
}