标签:str库函数
#include<stdio.h>
#include<assert.h>
char *my_strcat(char *dst,const char *src)
{
char *cp = dst;
assert(dst);
assert(src);
while(*cp)
{
cp++;
}
while(*cp++ = *src++)
{
;
}
return dst;
}
char strncat(char *front,const char *back,int count)
{
char *start = front;
assert(front);
assert(back);
while(*front)
{
front++;
}
while(count--)
{
if(!(*front++ = *back++))//判断是否到back的末尾!
{ //当遇到0就返回的就在条件前面加"!"
return start;
}
}
*front = ‘\0‘;
return start;
}
int strcmp(const char *src,const char *dst)
{
int ret = 0;
assert(src);
assert(dst);
while(!(ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
{
src++;
dst++;
}
return ret;
}
int strncmp(const char *first,const char *last,int count)//compare first n characters of two strings
{
assert(first);
assert(last);
if(!count)
{
return 0;
}
while(--count && *first && *first == *last)
{
first++;
last++;
}
return (*(unsigned char *)first - *(unsigned char *)last);
}
int strlen(const char *str)
{
const char *eos = str;
assert(str);
while(*eos++)
{
;
}
return ((int)(eos - str - 1));//指针减指针为它们之间的元素个数
}
char * strncpy(char *dst,const char *src,int count)
{
char *start = dst;
assert(dst);
assert(src);
while(count-- && (*dst = *src))
{
;
}
if(count)
{
while(--count)
{
*dest++ = ‘\0‘;
}
}
return start;
}
char *strcpy(char *dst,const char *src)
{
char *start = dst;
assert(dst);
assert(src);
while(*dst++ = *src++)
{
;
}
return start;
}
本文出自 “水仙花” 博客,请务必保留此出处http://10704527.blog.51cto.com/10694527/1708936
标签:str库函数
原文地址:http://10704527.blog.51cto.com/10694527/1708936