目录
- strlen
- strcpy
- strcat
- strcmp
- strstr
- strtok
- strerror与perror
- memcpy
- memmove
- memset
- atoi
strlen
int my_strlen1(const char* s)
{
assert(s);
int count = 0;
while (*s != '\0')
{
count++;
s++;
}
return (unsigned)count;
}
int my_strlen2(const char* s)
{
if (*s == '\0')
return 0;
return 1 + my_strlen2(++s);
}
int main()
{
char str1[] = "abcdefg";
printf("%d\n", my_strlen2(str1));
return 0;
}
strcpy
char* my_strcpy(char* s1, const char* s2)
{
assert(s1 && s2);
char* ret = s1;
while (*s1++ = *s2++)
{
;
}
return ret;
}
int main()
{
char str1[20];
char str2[] = "abcdefg";
my_strcpy(str1, str2);
printf("%s\n", str1);
return 0;
}
strcat
char* my_strcat(char* s1, const char* s2)
{
assert(s1 && s2);
char* ret = s1;
while (*s1 != '\0')
{
s1++;
}
while (*s1++ = *s2++)
{
;
}
return ret;
}
int main()
{
char str1[20] = "123";
char str2[] = "abcdefg";
my_strcat(str1, str2);
printf("%s\n", str1);
return 0;
}
strcmp
int my_strcmp(const char* s1, const char* s2)
{
while(*s1)
{
if (*s1 != *s2)
break;
s1++;
s2++;
}
return *s1 - *s2;
}
int main()
{
char str1[] = "abc";
char str2[] = "abcd";
printf("%d\n", my_strcmp(str1, str2));
return 0;
}
strstr
char* my_strstr(const char* s1, const char* s2)
{
assert(s1 && s2);
char* cur = s1;
char* str2 = s2;
while (cur)
{
s1 = cur;
s2 = str2;
while (*s1 == *s2)
{
s1++;
s2++;
}
if (*s2 == '\0')
return cur;
cur++;
}
return NULL;
}
int main()
{
char str1[] = "abbcacabcdabd";
char str2[] = "abcd";
printf("%s\n", my_strstr(str1, str2));
return 0;
}
strtok
int main()
{
char str1[] = "111@[email protected]. 555/666";
char sep[] = "@. /";
char* i = NULL;
for (i = strtok(str1, sep); i != NULL; i = strtok(NULL, sep))
{
printf("%s\n", i);
}
return 0;
}
strerror与perror
int main()
{
printf("%s\n", strerror(0));
printf("%s\n", strerror(1));
printf("%s\n", strerror(2));
printf("%s\n", strerror(3));
int* p = (int*)malloc(INT_MAX);
if (p == NULL)
{
printf("%s\n", strerror(errno));
perror("Malloc");
return 1;
}
return 0;
}
memcpy
void* my_memcpy(void* des, const void* src, size_t num)
{
assert(des && src);
void* ret = des;
while (num--)
{
*(char*)des = *(char*)src;
des = (char*)des + 1;
src = (char*)src + 1;
}
return ret;
}
int main()
{
int arr1[] = { 0,1,2,3,4,5,6,7,8,9 };
int arr2[] = { 10,10,10,10,10,10,10 };
my_memcpy(arr1+2, arr1, sizeof(int) * 4);
Print(arr1, sizeof(arr1) / sizeof(int));
return 0;
}
memmove
void* my_memmove(void* des, const void* src, size_t num)
{
void* ret = des;
if (des > src)
{
while (num--)
{
*((char*)des + num) = *((char*)src + num);
}
}
else
{
while (num--)
{
*(char*)des = *(char*)src;
des = (char*)des + 1;
src = (char*)src + 1;
}
}
return ret;
}
int main()
{
int arr1[] = { 0,1,2,3,4,5,6,7,8,9 };
my_memmove(arr1 + 2, arr1, sizeof(int) * 4);
return 0;
}
memset
int main()
{
int arr[] = { 0x11111111,0x22222222,3,4,5 };
memset(arr, 6, 20);
return 0;
}
atoi
#include
#include
#include
#include
#include
enum Status
{
VALUE,
INVALUE
}status = INVALUE;
int my_atoi(const char* s)
{
assert(s);
int flag = 1;
if (*s == '\0')
{
return 0;
}
while (isspace(*s))
{
s++;
}
if (*s == '+')
{
flag = 1;
s++;
}
else if (*s == '-')
{
flag = -1;
s++;
}
long long n = 0;
while (*s != '\0')
{
if (isdigit(*s))
{
n = n * 10 + flag * (*s - '0');
if (n<INT_MIN)
{
n = INT_MIN;
break;
}
else if(n > INT_MAX)
{
n = INT_MAX;
break;
}
}
else
{
n = 0;
break;
}
s++;
}
if (*s == '\0')
{
status = VALUE;
}
return (int)n;
}
int main()
{
char a[20] = " +1111111aa1111123456";
int ret = my_atoi(a);
if (status == VALUE)
{
printf("合法的:%d\n", ret);
}
else
{
printf("非法的%d\n", ret);
}
return 0;
}