Strcpy 2. 1. achieve achieve achieve strcat 3. strstr 4. implemented strchr 5. implemented strcmp 6. implemented to achieve memcpy 7. memmove

1. To achieve strcpy

Copy the string starts and containing a NULL terminator from src to dest address to the start address space

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>



char *mystrcpy(char *str2, const char *str1)
{
	assert(str1 != NULL);
	assert(str2 != NULL);

	char *r = str2;
	while ((*r++ = *str1++) != '\0');

	return r;
}

int main()
{
	char str1[] = "abcdef";
	char str2[] = { 0 };
	mystrcpy(str2, str1);
	strcpy(str2, str1);

	printf("%s\n", str2);
	printf("%s\n", str2);


	return 0;
}


2. To achieve strcat

Src points to a character string (including "\ 0") copied to the string pointed to by dest (delete * dest end of the original "\ 0"). To ensure * dest long enough to accommodate the incoming copy * src. * src any original characters intact. Returns a pointer to the dest pointers .

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

char *mystrcat(char *str2, const char *str1)
{
	assert(str1);
	assert(str2);
	char *r = str2;
	while (*str2 != '\0')
	{
		str2++;
	}

	while (*str2++ = *str1++);
	
	return str2;
}

int main()
{
	char str1[] = "def";
	char str2[] = "abc";
	mystrcat(str2, str1);
	strcat(str2, str1);

	printf("%s\n", str2);
	printf("%s\n", str2);


	return 0;
}


3. Implementing strstr

strstr (str1, str2) function is used to determine whether a string is a substring of str1 str2. If so, then the function returns the address of the first occurrence of str2 in str1; otherwise, returns NULL.

#include <stdio.h>
#include <string.h>

char *mystrstr(const char *str1, const char *str2)
{
	char *cp = (char*)str1;
	char *s1, *s2;
	if (!*str2)
		return((char *)str1);
	while (*cp)
	{
		s1 = cp;
		s2 = (char *)str2;
		while (*s1 && *s2 && !(*s1 - *s2))
			s1++, s2++;
		if (!*s2)
			return(cp);
		cp++;
	}
	return(NULL);
}

int main()
{
	char str1[] = "1234xyz";
	char str2[] = "34";
	printf("%s\n", mystrstr(str1, str2));
	printf("%s\n", strstr(str1, str2));


	return 0;
}


4. Implement strchr

strchr functional capabilities as the first place to find a match in a given character string. Function prototype: char * strchr (const char * str, int c), i.e., a first search character C appears (an unsigned char) in the first position of the string pointed to by the argument str.

#include <stdio.h>
#include <string.h>


char *mystrchr(const char *str, int c)
{
	while (*str != '\0' && *str!= c)
	{
		++str;
	}
	return (*str == c )? str : NULL;
}


int main()
{
	char str[] = "abcdefd";
	char c = 'd';
	printf("%s\n", mystrchr(str, c));
	printf("%s\n", strchr(str, c));


	return 0;
}


5. Implement strcmp

strcpy is a C language standard library function, comprising the strcpy '\ 0' copy to another end of a character string of the address space , the type of the return value is char *.

#include <stdio.h>
#include <string.h>
#include <assert.h>

int mystrcmp(const char *str1, const char *str2)
{
	while (*str1 == *str2)
	{
		assert((str1 != NULL) && (str2 != NULL));
		if (*str1 == '\0')
			return 0;
		str1++;
		str2++;
	}
	if (*str1 > *str2)
		return 1;
	else
		return -1;
}


int main()
{
	char str1[] = "abcdefd";
	char str2[] = "abcd";
	printf("%d\n", mystrcmp(str1, str2));
	printf("%d\n", strcmp(str1, str2));


	return 0;
}


6. Implement memcpy

memcpy memory copy function refers to the use of C ++ and C, the function prototype void * memcpy (void * destin, void * source, unsigned n); function is the function from a starting position to begin copying the source memory address several bytes the target memory address, i.e., the copy source n bytes from the source into the target destin.

#include <stdio.h>
#include <string.h>
#include <assert.h>

void *mymemcpy(void * dst, const void * src, size_t count)
{
	void * ret = dst;
	assert(dst);
	assert(src);
	while (count--)
	{
		*(char *)dst = *(char *)src;
		dst = (char *)dst + 1;
		src = (char *)src + 1;
	}

	return(ret);
}
	

int main()
{
	const char *s1 = "hello";
	int a = sizeof(s1);
	char s2[1024]= { 0 };
	mymemcpy(s2, s1, strlen(s1));
	printf("%s\n", s2);
	memcpy(s2, s1, strlen(s1));
	printf("%s\n", s2);	

	return 0;
}


7. achieve memmove

memmove for copying bytes, if the target region and the source region overlap, then, to ensure that the source string memmove overlapping byte area copied to the target area before being covered, but after the copy source contents are changed. However, when the target region and the source region and does not overlap the same function memcpy function.

#include <stdio.h>
#include <string.h>
#include <assert.h>



void *mymemmove(void * dst, const void * src, size_t count)
{
	void * ret = dst;

	if (dst <= src || (char *)dst >= ((char *)src + count))
	{
		while (count--)
		{
			*(char *)dst = *(char *)src;
			dst = (char *)dst + 1;
			src = (char *)src + 1;
		}
	}
	else
	{
		dst = (char *)dst + count - 1;
		src = (char *)src + count - 1;
		while (count--)
		{
			*(char *)dst = *(char *)src;
			dst = (char *)dst - 1;
			src = (char *)src - 1;
		}
	}

	return(ret);
}


int main()
{
	char buffer1[] = "abcdef";
	char buffer2[] = "ddadfg";

	mymemmove(buffer1, buffer2, sizeof(buffer1));
	printf("%s\n", buffer1);
	memmove(buffer1, buffer2, sizeof(buffer1));
	printf("%s\n", buffer1);

	return 0;
}

 

Guess you like

Origin blog.csdn.net/qq_43210641/article/details/94744683