求字符串长度
//strlen() 模拟
// 求字符串长度,必须以'\0'结尾
//size_t strlen( const char* str);
size_t my_strlen(const char* str)
{
int count = 0;
while (*str != '\0')
{
count++;
str++;
}
return count;
}
拷贝字符串
//strcpy() 模拟
// 字符串拷贝
//char* strcpy(char *destination,const char *source);
char* my_strcpy(char *destination, const char* source)
{
assert(destination != NULL && source != NULL);
char* strdes = destination;
const char* strsou = source;
while (*strsou != '\0')
{
*strdes = *strsou;
strdes++;
strsou++;
}
*strdes = '\0';
return destination;
}
连接字符串
//strcat() 模拟
// 连接两个字符串,destination必须要有足够的空间
//char* strcat(char *destination,const char *source);
char* my_strcat(char* destination, const char* source)
{
assert(destination != NULL && source != NULL && destination!=source);
char* strdes = destination;
const char* strsou = source;
while (*strdes != '\0')
{
strdes++;
}
while (*strsou != '\0')
{
*strdes = *strsou;
strdes++;
strsou++;
}
*strdes = '\0';
return destination;
}
比较字符串
//strcmp() 模拟
// 两个字符串比较
//int strcmp(const char*str1,const char*str2);
int my_strcmp(const char* str1, const char* str2)
{
assert(str1 != NULL && str2 != NULL);
while (*str1!='\0')
{
if (*str1 == *str2)
{
str1++;
str2++;
}
else
break;
}
return *str1 - *str2;
}
查找字符串1中,第一个出现字符串2的位置,返回其指针
//strstr() 模拟
// 查找str1中第一个出现str2字符串的位置
//char * strstr ( const char *, const char * );
char* my_strstr(const char* str1, const char* str2)
{
assert(str1 != NULL && str2 != NULL);
const char* s1 = str1;
const char* s2 = str2;
while (*s1 != '\0'&& *s2 != '\0')
{
if (*s1 == *s2)
{
s2++;
s1++;
}
else
{
s2 = str2;
s1++;
}
}
if (*s2 == '\0')
return (s1 - strlen(str2));
else
return NULL;
}
char* my_strncpy( char *strDest, const char *strSource, size_t count )
{
assert(strDest!=NULL && strSource!=NULL);
char *pDest = strDest;
int str_len = strlen(strSource);
for(int i=0; i
char* my_strncat( char *strDest, const char *strSource, size_t count )
{
assert(strDest!=NULL && strSource!=NULL);
char *pDest = strDest;
while(*pDest != '\0')
pDest++;
int str_len = strlen(strSource);
for(int i=0; i
int my_strncmp( const char *string1, const char *string2, size_t count )
{
assert(string1!=NULL && string2!=NULL);
int res = 0;
while(count-- != 0)
{
res = *string1 - *string2;
if(res != 0)
break;
string1++;
string2++;
}
if(res > 0)
res = 1;
else if(res < 0)
res = -1;
return res;
}
//char *strtok( char *strToken, const char *strDelimit );
int main()
{
//char *p = "zhangsan\0qq.com";
char p[] = "[email protected]";
const char* sep = "@.";
strtok(p, sep);
char *res = strtok(NULL, sep);
printf("%s\n", res);
}
返回错误码,所对应的错误信息。
#include
#incldue
//errno
void main()
{
FILE *fp = fopen("Test1.txt", "r");
if(fp == NULL)
{
printf("errno = %d\n", errno);
printf("err msg = %s\n", strerror(errno));
printf("打开文件失败.\n");
return;
}
printf("打开文件成功.\n");
fclose(fp);
}
拷贝source中的n个字节到destination()
//memcpy() 模拟
// 复制source中n个字节的内容到destination
// 如果source和destination有任何的重叠,复制的结果都是未定义的。
//void * memcpy ( void * destination, const void * source, size_t num );
void* my_memcpy(void* destination, const void* source, size_t num)
{
assert(destination != NULL && source != NULL);
char* strdes = (char*)destination;
char* strsou = (char*)source;
while (num--)
{
*strdes = *strsou;
strdes++;
strsou++;
}
return destination;
}
拷贝source中的n个字节到destination()
//memmove() 模拟
//复制source中n个字节的内容到destination
//如果source和destination有任何的重叠,复制的结果不会出错。
//void * memmove ( void * destination, const void * source, size_t num );
void* my_memmove(void* destination, const void* source, size_t num)
{
assert(destination != NULL && source != NULL);
char* strdes = (char*)destination;
char* strsou = (char*)source;
//destination在后,source在前,发生空间重叠,倒着复制
if (strdes > strsou)
{
strdes = strdes + num - 1;
strsou = strsou + num - 1;
while (num--)
{
*strdes = *strsou;
strdes--;
strsou--;
}
}
else
{
while (num--)
{
*strdes = *strsou;
strdes++;
strsou++;
}
}
return destination;
}
比较从ptr1和ptr2指针开始的num个字节
int my_memcmp( const void *buf1, const void *buf2, size_t count )
{
assert(buf1!=NULL && buf2!=NULL);
const char *pf1 = (const char*)buf1;
const char *pf2 = (const char *)buf2;
int res = 0;
while(count-- != 0)
{
res = *pf1 - *pf2;
if(res != 0)
break;
pf1++;
pf2++;
}
return res;
}
初始化dest指向的地址及其后n个字节;
void* my_memset( void *dest, int c, size_t count )
{
assert(dest != NULL);
char *pDest = (char *)dest;
while(count-- != 0)
{
*pDest++ = c;
}
return dest;
}