1.strcpy
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<windows.h>
#include<assert.h>
#include<string.h>
char* my_strcpy(char *dest, const char *scr)
{
char *ret=dest;
assert(dest);
assert(scr);
while(*dest++=*scr++)
{
;
}
return ret;
}
int main()
{
const char str1[5]="abcd";
char str2[5]={0};
printf("%s\n",my_strcpy(str2,str1)) ;
system("pause");
return 0;
}
2.strncpy
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<windows.h>
#include<assert.h>
#include<string.h>
char *my_strncpy( char *dest, const char *src, int len )
{
char *ret=dest;
assert(dest);
assert(src);
while(len&&*src)
{
*dest++=*src++;
len--;
}
/*if(len!=0)
{
while(len)
{
*dest++='\n';
len--;
}
}*/
return dest;
}
int main()
{ char string[50]="I am a student!";
const char str1[3]="he";
const char str2[3]="is";
const char str3[10]="teacher";
my_strncpy(string,str1,2);
my_strncpy(string+2,str2,2);
my_strncpy(string+7,str3,7);
printf("%s\n",string);
system("pause");
return 0;
}
3.strcat
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<windows.h>
#include<assert.h>
#include<string.h>
char *my_strcat( char *dest, const char *src )
{
char *ret=dest;
assert(dest);
assert(src);
while(*dest)
{
*dest++;
}
while(*dest++=*src++)
{
;
}
return ret;
}
int main()
{
const char str1[]= "you are";
char str2[]=" a teacher";
printf("%s",my_strcat(str1,str2));
system("pause");
return 0;
}
4.strncat
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<windows.h>
#include<assert.h>
#include<string.h>
char *my_strncat(char *dest,const char *src,int count)
{
char *ret=dest;
assert(dest);
assert(src);
while(*dest){
dest++;
}
while(count&&(*dest++=*src++)){
count--;
}
return ret;
}
int main()
{
char str1[20]="hello";
my_strncat(str1," world",6);
printf("%s\n",str1);
system("pause");
return 0;
}
5.strcmp
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<windows.h>
#include<assert.h>
#include<string.h>
int My_strcmp( const char *str1, const char *str2)
{
assert(str1);
assert(str2);
while(*str1!='\0'&& *str2!='\0')
{
if(*str1==*str2)
{
str1++;
str2++;
}
if(*str1>*str2)
{
return 1;
}
if(*str1<*str2)
{
return -1;
}
if(*str1=='\0'&&*str2!='\0')
{
return -1;
}
if(*str2=='\0'&&*str1!='\0')
{
return -1;
}
return 0;
}}
int main()
{
const char *str1="student!";
const char *str2="teacher!!";
if( (My_strcmp(str1, str2))==0)
{
printf("两个字符串相等\n");
}
if( (My_strcmp(str1, str2))==1)
{
printf("str1大于str2\n");}
if((My_strcmp(str1, str2))==-1)
{
printf("str1小于str2\n");
}
system("pause");
return 0;
}
6.strncmp
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<windows.h>
#include<assert.h>
#include<string.h>
int My_strncmp( const char *str1, const char *str2, int len )
{
assert(str1);
assert(str2);
while(len--)
{
while(*str1!='\0'&& *str2!='\0')
{
if(*str1==*str2)
{
str1++;
str2++;
}
if(*str1>*str2)
{
return 1;
}
if(*str1<*str2)
{
return -1;
}
if(*str1=='\0'&&*str2!='\0')
{
return -1;
}
if(*str2=='\0'&&*str1!='\0')
{
return -1;
}
return 0;
}}}
int main()
{
const char *str1="student!!";
const char *str2="teacher!!";
if( (My_strncmp(str1, str2,2))==0)
{
printf("两个字符串相等\n");
}
if( (My_strncmp(str1, str2,2))==1)
{
printf("str1大于str2\n");}
if((My_strncmp(str1, str2,2))==-1)
{
printf("str1小于str2\n");
}
system("pause");
return 0;
}
7.memcpy
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<windows.h>
#include<assert.h>
#include<string.h>
void *my_memcpy( void *dest, const void *src, size_t count )
{
char *ret=dest;
while(count--)
{
assert(dest);
*(char*)dest = *(char *)src;
dest = (char*)dest + 1;
src = (char *)src + 1;
}
return dest;
}
int main()
{
const char *str1="student!!";
char str2[]="abcdefg";
int num=0;
printf("befor:%s\n",str1);
printf("please enter:");
scanf("%d",&num);
printf("%s\n",my_memcpy( str2, str1, num ));
system("pause");
return 0;
}
8.memmove
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<windows.h>
#include<assert.h>
#include<string.h>
void *my_memmove( void *dest, const void *src, size_t count )
{
assert(src);
if (dest <= src || (char*)dest >= ((char*)src + count))
{
while (count--)
{
assert(dest);
*(char*)dest = *(char *)src;
dest = (char*)dest + 1;
src = (char *)src + 1;
}
}
else
{
(char*)dest = (char*)dest + count - 1;
(char *)src = (char *)src + count - 1;
while (count--)
{
*(char*)dest = *(char *)src;
dest = (char*)dest - 1;
src = (char *)src - 1;
}
}
int main()
{
char *src = "AABCDE";
char dest[] = "AAAAABBBB";
printf("befor: %s\n",dest);
my_memmove(dest, src, strlen(src) + 1);
printf("enter: %s", dest);
system("pause");
return 0;
}