strcpy
#include<stdio.h>
#include<assert.h>
#include<windows.h>
char *MyStrcpy(char *arr1, char *arr2)
{
assert(NULL != *arr1);
assert(NULL != *arr2);
int *ret = arr1;
while (*arr1++ = *arr2++)
{
;
}
printf("%s\n", ret);
}`在这里插入代码片`
int main()
{
char arr1[30] = "hello";
char arr2[30] = "world";
int ret = MyStrcpy(arr1, arr2);
system("pause");
return 0;
}
strcpy:字符串拷贝,是从缓冲区开始拷贝的。
用法如下char * strcpy ( char * destination, const char * source );
int *ret=arr1 是先将arr1的内容保存下来,循环以后输出ret的值。
①=②
①
while (*arr1++ = *arr2++)
{
;
}
printf("%s\n", ret);
②
while (arr1 = arr2)
{
arr1++;
arr2++;
}
strcat
#include<stdio.h>
#include<assert.h>
#include<windows.h>
char *MyStract(char *arr1, char *arr2)
{
assert(NULL != arr1);
assert(NULL != arr2);
char *ret = arr1;
while (*arr1)
{
arr1++;
}
while (*arr1++ = *arr2++)
{
;
}
printf("%s\n", ret);
}
int main()
{
char arr1[20] = "hello ";
char arr2[20] = "world!";
MyStract(arr1, arr2);
system("pause");
return 0;
}
strcat:字符串拼接
用法如下:char * strcat ( char * destination, const char * source );
跟字符串拷贝类似,当arr1到了‘\0’时,返回arr1,再进行字符串拷贝工作,将arr2拷贝到arr1的\0之后。
strcmp
#include<stdio.h>
#include<assert.h>
#include<windows.h>
int MyStrcmp(char *arr1, char *arr2)
{
assert(NULL != *arr1);
assert(NULL != *arr2);
int ret = 0;
while (!(ret=*(unsigned char*)arr1-*(unsigned char*)arr2)&&arr1&&arr2)
{
arr1++;
arr2++;
}
if (ret>0)
{
ret = 1;
}
else
{
ret = -1;
}
return ret;
}
int main()
{
char arr1[30] = "abcdef";
char arr2[30] = "abcde";
int ret=MyStrcmp(arr1, arr2);
printf("%d\n", ret);
system("pause");
return 0;
}
strcmp:字符串比较。根据ASCII码判定,判定规则从左到右,与字符串长短无关。
用法:int strcmp ( const char * str1, const char * str2 );while判定条件里表示,两个字符串相减是否等于0,并且两个字符串是否已经到达\0,如果其相减为0,或者某一个字符串碰到了\0,则循环结束,否则,字符串++。循环跳出后,用if语句根据返回值的征服,判断字符串的大小。
strstr
#include<stdio.h>
#include<windows.h>
char *MyStrstr(char *arr1, char *arr2)
{
char *p1 = arr1;
char *p2 = arr2;
char *pt = arr1;
while (strlen(pt) >= strlen(p2))
{
p1 = pt;
while ((*p1 == *p2) && (*p2 != '\0'))
{
p1++;
p2++;
}
if (*p2 == '\0')
return pt;
p2 = arr2;
pt++;
}
return NULL;
}
int main()
{
char *arr1 = "abcdef";
char *arr2 = "de";
printf("%s\n", MyStrstr(arr1, arr2));
system("pause");
return 0;
}
strstr:子串查找
用法:const char * strstr ( const char * str1, const char * str2 ); char * strstr ( char * str1, const char * str2 );
首先,在arr1里面找arr2,则arr1字符串一定要比arr2字符串更长,while循环将找到的字符串与其后面的字符串一起返回,没有找到,则返回空。
memcpy
#include<stdio.h>
#include<assert.h>
#include<windows.h>
void *MyMemcpy(void *arr1, void *arr2, size_t count)
{
assert(NULL != arr1);
assert(NULL != arr2);
void *ret = arr1;
while (count)
{
*(char*)arr1 = *(char*)arr2;
arr1 = (char*)arr1 + 1;
arr2 = (char*)arr2 + 1;
count--;
}
return ret;
}
int main()
{
int arr1[5] = { 0 };
int arr2[5] = { 1, 2, 3, 4, 5 };
MyMemcpy(arr1, arr2, 12);
for (int i = 0; i < 5; i++)
{
printf("%d ", arr1[i]);
}
printf("\n");
system("pause");
return 0;
}
memcpy:men系列的函数叫做内存操作函数本质上不关心类型,只关心操作的字节数,即操作的基本单位是字节。
用法:void * memcpy ( void * destination, const void * source, size_t num );
memmove
#include<stdio.h>
#include<assert.h>
#include<windows.h>
void *MyMemmove(void *dst, const void *src, size_t num)
{
assert(dst != NULL);
assert(src != NULL);
const char *src_p = (char *)src;
char *dst_p = (char *)dst;
if (dst_p > src_p && dst_p < src_p + num)
{
src_p = src_p + num - 1;
dst_p = dst_p + num - 1;
while (num > 0)
{
*dst_p = *src_p;
dst_p--;
src_p--;
num--;
}
}
else
{
while (num > 0)
{
*dst_p = *src_p;
dst_p++;
src_p++;
num--;
}
}
return dst;
}
memmove:内存重叠函数,在面试中常出现
用法:void * memmove ( void * destination, const void * source, size_t num );
第一部分if判定的情况是从右向左重叠的情况,else判定的情况是正常从左向右重叠的情况拷贝的情况是类似的,所以while循环基本相似。