实现strcpy
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char *Strcpy(char*dest, const char*src)
{
char*p = dest;
assert(dest != NULL&&src != NULL);
while (*dest++ = *src++);
return p;
}
int main()
{
char dest[20] = { 0 };
char *src = "hehe";
Strcpy(dest, src);
printf("%s\n", dest);
system("pause");
return 0;
}
实现strncpy
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char *strncpy(char *dest, const char*src, int n)
{
int count = 0;
char*p = dest;
assert(dest != null&&src != null);
while (count < n)
{
*dest++ = *src++;
count++;
}
return p;
}
int main()
{
char dest[20] = "hehe";
char*src = "nuonuo";
strncpy(dest, src, 3);
printf("%s\n", dest);
system("pause");
return 0;
}
实现strcat
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char*Strcat(char*dest, const char*src)
{
char*p = dest;
assert(dest != NULL&&src != NULL);
while (*dest != '\0')
{
dest++;
}
while (*dest++ = *src++);
return p;
}
int main()
{
char dest[20] = "hehe";
char*src = "haha";
Strcat(dest, src);
printf("%s\n", dest);
system("pause");
return 0;
}
实现strncat
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char Strncat(char*dest, const char*src, int n)
{
char*p = dest;
int count = 0;
assert(dest != NULL&&src != NULL);
while (*dest != '\0')
{
dest++;
}
while (count < n)
{
*dest++ = *src++;
count++;
}
*dest = '\0';
return p;
}
int main()
{
char str[10] = "abcd";
char *src = "hello";
Strncat(str, src, 2);
printf("%s\n", str);
system("pause");
return 0;
}
实现strcmp
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char Strcmp(const char*str1, const char*str2)
{
assert(str1 != NULL&&str2 != NULL);
while (*str1 == *str2)
{
if (*str1== '\0')
{
return 0;
}
str1++;
str2++;
}
if (*str1 > *str2)
{
return 1;
}
else
{
return -1;
}
}
int main()
{
char *str1 = "abcd";
char *str2 = "ab";
int n = Strcmp(str1, str2);
printf("%d\n", n);
system("pause");
return 0;
}
实现strncmp
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char Strncmp(const char*str1, const char*str2, int n)
{
int diff = 0;
int count = 0;
assert(str1 != NULL&&str2 != NULL);
while (count < n&&((diff =( *str1 - *str2)) == 0) && *str1 != '\0')
{
str1++;
str2++;
count++;
}
return diff;
}
int main()
{
char *str1 = "ad";
char *str2 = "acd";
int n = Strncmp(str1, str2, 2);
printf("%d\n", n);
system("pause");
return 0;
}
实现strstr
# include<stdio.h>
#include<stdlib.h>
#include<assert.h>
const char *Strstr(const char*str1, const char*str2)
{
const char*s1 = str1;
const char*s2 = str2;
const char *start = s1;
assert(str1 != NULL&&str2 != NULL);
while (*start != '\0')
{
s1 = start;
s2 = str2;
while (*s1 != '\0'&&*s2 != '\0'&&*s1 == *s2)
{
s1++;
s2++;
}
if (*s1 == '\0')
{
return NULL;
}
if (*s2 == '\0')
{
return start;
}
start++;
}
return NULL;
}
int main()
{
char *str1 = "ABCDDTTFF";
char *str2 = "CDDT";
const char *p = Strstr(str1, str2);
printf("%s\n", p);
system("pause");
return 0;
}
实现strchr
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char* Strchr(const char* str, int character) {
assert(str != NULL);
for (; *str != '\0'; ++str) {
if (*str == (char)character)
return str;
}
if (*str == '\0') {
return NULL;
}
}
int main() {
char str[] = "hello world";
char* pch;
printf("查找'l'符号在字符串\%s中 \n", str);
pch = Strchr(str, 'l');
while (pch != NULL) {
printf("found at %d\n", pch - str + 1);
pch = Strchr((pch + 1), 'l');
}
system("pause");
return 0;
}
实现memcpy
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
void* Memcpy(void* dest, const void* src, size_t num) {
assert(dest != NULL&&src!=NULL);
const char* psrc = (const char*)src;
char* pdest = (char*)dest;
for (size_t i = 0; i < num; ++i) {
pdest[i] = psrc[i];
}
return dest;
}
int main() {
int arr1[4] = { 0 };
int arr2[4] = { 1,2,3,4 };
Memcpy(arr1, arr2, 16);//单位是字节,所以不能填4
for (int i = 0; i < 4; ++i) {
printf("%d ", arr1[i]);
}
system("pause");
return 0;
}
实现memmove
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
void* Memmove(void* dest, const void* src, size_t num) {
assert(dest != NULL);
assert(src != NULL);
const char* psrc = (const char*)src;
char* pdest = (char*)dest;
if (pdest<psrc || pdest>psrc + num) {//判断是否存在内存重叠情况
for (size_t i = 0; i < num; ++i) {
pdest[i] = psrc[i];
}
}
else {
for (size_t i = num; i > 0; --i) {
pdest[i] = psrc[i];
}
}
return dest;
}
int main() {
char str[] = "memmove can be very useful......";
Memmove(str + 20, str + 15, 11);
puts(str);
system("pause");
return 0;
}