size_t strlen ( const char * str );
//模拟实现的strlen()函数命名位my_strlen()函数
//模拟实现方法一
//size_t my_strlen(const char* str)
//{
// assert(str);
// int count = 0;
// while (*str != '\0')
// {
// count++;
// str++;
// }
// return count;
//}
//模拟实现方法二:指针-指针
//size_t my_strlen(const char* str)
//{
// assert(str);
// char* start = str;
// while (*str)
// {
// str++;
// }
// return str - start;
//}
//模拟实现方法三:递归
size_t my_strlen(const char* str)
{
assert(str);
if (*str == '\0')
return 0;
else
return 1 + my_strlen(str + 1);
}
char * strcpy ( char * destination, const char * source );
//模拟实现的strcpy()函数命名位my_strcpy()函数
char* my_strcpy(char* dest, const char* src)
{
assert(dest && src);
char* ret = dest;
while (*dest++ = *src++)
{
;
}
return ret;
}
char * strcat ( char * destination, const char * source );
char* my_strcat(char* dest, const char* src)
{
assert(dest && src);
char* ret = dest;
//1.找到目标空间'\0'
while (*dest)
{
dest++;
}
//2. 追加字符
while (*dest++ = *src++)
{
;
}
return ret;
}
int strcmp ( const char * str1, const char * str2 );
//模拟实现的strcmp()函数命名位my_strcmp()函数
int my_strcmp(const char* str1, const char* str2)
{
assert(str1 && str2);
while (*str1 == *str2)
{
str1++;
str2++;
}
return *str1 - *str2;
}
char * strncpy ( char * destination, const char * source, size_t num );
char * strncat ( char * destination, const char * source, size_t num );
int strncmp ( const char * str1, const char * str2, size_t num );
const char * strstr ( const char * str1, const char * str2 );
//模拟实现的strstr()函数命名位my_strstr()函数
char* my_strstr(const char* str1, const char* str2)
{
assert(str1 && str2);
char* cp = str1;
char* s1 = cp;
char* s2 = str2;
if (*s2 == '\0')
return NULL;
while (*cp)
{
//开始匹配
s1 = cp;
s2 = str2;
while(*s1 && *s2 && *s1 == *s2)
{
s1++;
s2++;
}
if (*s2 == '\0')
return cp;
cp++;
}
return NULL;
}
char * strtok ( char * str, const char * sep );
int main()
{
char arr1[] = "[email protected]";
char copy[30];
strcpy(copy, arr1);
char arr2[] = "@.";
char* ret = NULL;
for (ret = strtok(copy, arr2); ret != NULL; ret = strtok(NULL, arr2))
{
printf("%s\n", ret);
}
return 0;
}
库函数在执行时候,发生错误会将一个错误码存放在errno这个变量中。而errno是C语言提供的一个全局变量。
char * strerror ( int errnum );
#include
#include
int main()
{
for (int i = 0; i < 10; i++)
{
printf("%d: %s\n", i,strerror(i));
}
return 0;
}
int tolower (int c)
int toupper (int c)
void * memcpy ( void * destination, const void * source, size_t num );
void* my_memcpy(void* dest, const void* src, size_t num)
{
assert(dest && src);
void* ret = dest;
while (num--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
return ret;
}
void * memmove ( void * destination, const void * source, size_t num );
void* my_memmove(void* dest, const void* src, size_t num)
{
assert(dest && src);
void* ret = dest;
if (dest > src)//从后向前拷贝
{
while (num--)
{
*((char*)dest + num) = *((char*)src + num);
}
}
else//从前向后拷贝
{
while (num--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
}
return ret;
}
int memcmp ( const void * ptr1, const void * ptr2, size_t num );