C语言库函数实现
strcpy
#include
#include
unsigned char* myStrcpy(unsigned char *dest,unsigned char *src)
{
assert(dest!=NULL&&src!=NULL);
unsigned char *bak=dest;
while(*src)
{
*dest++=*src++;
}
*dest='\0';
return bak;
}
unsigned char* myStrcpy2(unsigned char *dest,unsigned char *src)
{
assert(dest!=NULL&&src!=NULL);
unsigned char *bak=dest;
while((*dest++=*src++));
*dest='\0';
return bak;
}
unsigned char* myStrcpy3(unsigned char *dest,unsigned char *src)
{
assert(dest!=NULL&&src!=NULL);
unsigned char *bak=dest;
while(*src)
{
*dest=*src;
dest++;
src++;
}
*dest='\0';
return bak;
}
int main()
{
unsigned char str[128]={'\0'};
unsigned char *p="xiewenhui";
myStrcpy3(str,p);
puts(str);
return 0;
}
strncpy
#include
#include
unsigned char* myStrncpy(unsigned char *dest,unsigned char *src,int count)
{
assert(dest!=NULL&&src!=NULL);
char *bak=dest;
while(*src&&count>0)
{
*dest++=*src++;
count--;
}
if(count>0)
{
while(count>0)
{
count--;
*dest++='\0';
}
return dest;
}
*dest='\0';
return bak;
}
int main()
{
unsigned char str[128]={'\0'};
unsigned char *p="xiewenhui";
myStrncpy(str,p,15);
puts(str);
return 0;
}
strcat
#include
#include
#include
unsigned char* myStrcat(unsigned char *dest,unsigned char *src)
{
assert(dest!=NULL&&src!=NULL);
unsigned char *bak=dest;
while(*dest)
{
dest++;
}
while(*src)
{
*dest++=*src++;
}
*dest='\0';
return bak;
}
unsigned char* myStrcat2(unsigned char *dest,unsigned char *src)
{
assert(dest!=NULL&&src!=NULL);
unsigned char *bak=dest;
while(*dest)
{
dest++;
}
while((*dest++=*src++));
*dest='\0';
return bak;
}
unsigned char* myStrcat3(unsigned char *dest,unsigned char *src)
{
assert(dest!=NULL&&src!=NULL);
unsigned char* bak=dest;
strcpy(dest+strlen(dest),src);
return bak;
}
unsigned char* myStrcat4(unsigned char *dest,unsigned char *src)
{
assert(dest!=NULL&&src!=NULL);
unsigned char *bak=dest;
for(;*dest!='\0';dest++);
while((*dest++=*src++));
return bak;
}
int main()
{
unsigned char str[128]="xiewenhui";
unsigned char *p="handsome";
myStrcat4(str,p);
puts(str);
return 0;
}
strncat
#include
#include
unsigned char* myStrncat(unsigned char *dest,unsigned char *src,int count)
{
assert(dest!=NULL&&src!=NULL);
unsigned char *bak=dest;
while(*dest)
{
dest++;
}
while(*src&&count>0)
{
*dest++=*src++;
count--;
}
if(count>0)
{
while(count>0)
{
count--;
*dest++='\0';
}
return dest;
}
*dest='\0';
return bak;
}
int main()
{
unsigned char str[128]="xiewenhui";
unsigned char *p="handsome";
myStrncat(str,p,15);
puts(str);
return 0;
}
strcmp
#include
#include
int myStrcmp(const unsigned char *str1,const unsigned char *str2)
{
assert(str1!=NULL&&str2!=NULL);
int ret=0;
while(*str1&&str2&&(*str1==*str2))
{
str1++;
str2++;
}
ret=str1-str2;
if(ret>0)
{
return 1;
}
if(ret<0)
{
return -1;
}
return ret;
}
int main()
{
int ret=0;
unsigned char *str1="abcd";
unsigned char *str2="bbcd";
ret=myStrcmp(str1,str2);
if(ret==1)
{
printf("第一个字符串大于第二个字符串\n");
}
else if(ret==-1)
{
printf("第一个字符串小于第二个字符串\n");
}
else
{
printf("两个字符串相等\n");
}
return 0;
}
strncmp
#include
#include
int myStrncmp(const unsigned char* str1,const unsigned char* str2,int count)
{
assert(str1!=NULL&&str2!=NULL);
if(!count)
{
return 0;
}
while(*str1&&*str2&&(*str1==*str2)&&count--)
{
str1++;
str2++;
}
return *str1-*str2;
}
int main()
{
int ret=0;
unsigned char *str1="abcd";
unsigned char *str2="bbcd";
ret=myStrncmp(str1,str2,4);
printf("ret=%d\n",ret);
return 0;
}
strlen
#include
int myStrlen(unsigned char *str)
{
int len=0;
while(*str++)
{
len++;
}
return len;
}
int main()
{
int len=0;
unsigned char *p="xiewenhui";
len=myStrlen(p);
printf("len=%d\n",len);
}
strtok
#include
//#include
#include
char* myStrtok(char* p1, const char* p2)
{
assert(p2);
static char* pc = NULL;
char* ret = NULL;
char* p3 = (char*)p2;
if (p1 == NULL && !*pc)
return NULL;
if (p1 != NULL)
pc = p1;
while (*pc && *p2)//去掉前面要被剪掉的
if (*p2 == *pc)
{
pc++;
p2 = p3;
}
else
p2++;
ret = pc;//初始化
p2 = p3;
while (*pc && *pc != *p2++)//确定标记点
if (!*p2)
{
pc++;
p2 = p3;
}
if (*pc)//判断是不是最后的点
*pc++ = '\0';
return ret;
}
int main()
{
char str[128]="this is boy";
char *p=NULL;
printf("%s\n",p);
p=myStrtok(str," ");
while(p!=NULL)
{
printf("%s\n",p);
p=myStrtok(NULL," ");
}
return 0;
}
strstr
#include
#include
#include
char* myStrstr(char* str1,char* str2)
{
assert(str1!=NULL&&str2!=NULL);
char *bak1=str1;//保存字符串的首地址
char *bak2=str2;//保存子串的首地址
char *p=str1;
while(*p)
{
bak1=p;;
bak2=str2;
while(*bak1&&*bak2&&*bak1==*bak2)
{
bak1++;
bak2++;
}
if(*bak2=='\0')
{
return p;
}
p++;
}
return NULL;
}
int main()
{
char str[] = "This is a simple string";
char* pch;
pch = myStrstr(str, "simple");
if (pch != NULL)
strncpy(pch, "sample", 6);
puts(str);
return 0;
}
memcpy
#include
#include
void* myMemcpy(void *dest,void *src,int count)
{
assert(dest!=NULL&&src!=NULL);
void *bak=dest;
while(count--)
{
*(char*)dest++=*(char*)src++;
}
return bak;
}
int main()
{
unsigned char str[128]={0};
unsigned char *p="xiewenhui";
myMemcpy(str,p,8);
puts(str);
return 0;
}
memset
#include
void* myMemset(char *str,int n,int count)
{
unsigned char *bak=str;
while(count--)
{
*str++=n;
}
return str;
}
int main()
{
char str[128]="xiewenhui";
myMemset(str,'\0',sizeof(str));
printf("数据清空:%s",str);
return 0;
}
memmove
void *memmove(void *dest, const void *src, size_t count)
{
assert(dest != NULL || src != NULL)
if (dst < src)
{
char *p = (char *)dest;
char *q = (char *)src;
while (count--)
{
*p++ = *q++;
}
}
else
{
char *p = (char *)dest + count;
char *q = (char *)src + count;
while (count--)
{
*--p = *--q;
}
}
return dest;
}
atoi
int isspace(int x)
{
if (x == ' ' || x == '\t' || x == '\n' || x =='\f' || x =='\b' || x == '\r')
return 1;
else
return 0;
}
int isdigit(int x)
{
if (x <= '9' && x >= '0')
return 1;
else
return 0;
}
int my_atoi(const char *nptr)
{
int c; /*当前字符*/
int total; /*总计字节数*/
int sign; /*正数or负数*/
/*跳过开头的空格*/
while (isspace((unsigned char)*nptr))
++nptr;
c = (unsigned char)*nptr++;
sign = c; /*保存符号*/
if (c == '-' || c == '+')
c = (unsigned char)*nptr++; /* skip sign */
total = 0;
while (isdigit(c))
{
total = 10 * total + (c - '0'); /*求和*/
c = (unsigned char)*nptr++; /*获取下一个字符*/
}
if (sign == '-')
return -total;
else
return total;
}
itoa
char* myitoa(int value, char* str, int radix) {
static char dig[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int n = 0, neg = 0;
unsigned int v;
char* p, * q;
char c;
/* 负数:十进制直接在前面加负号,其它进制用补码 */
if (radix == 10 && value < 0) {
v = -value;
neg = 1;
}
else
{
v = value;
}
/* 其它进制的负数转为unsigned int类型,即取了补码 */
do {
str[n++] = dig[v % radix];
v /= radix;
} while (v);
if (neg)
str[n++] = '-';
str[n] = '\0';
/* 字符串反转 */
for (p = str, q = p + (n - 1); p < q; ++p, --q)
c = *p, * p = *q, * q = c;
return str;
}