字符串函数的模拟实现(strlen,strcapy,strcat,strstr,strcmp,memcpy,memmove))

字符串函数的模拟实现

1、strlen

#include <stdio.h>
#include <stdlib.h>

//方式一:计数器方式
size_t my_strlen1(char *str)
{
	size_t count = 0;
	while (*str != '\0')
	{
		count++;
		str++;
	}
	return count;
}

//方式二:不创建临时变量的递归实现
size_t my_strlen2(char *str)
{
	if (*str != '\0')
	{
		return 1 + my_strlen2(str + 1);
	}
	return 0;
}

//方式三:指针-指针实现
size_t my_strlen3(char *str)
{
	char *p = str;
	while (*p != '\0')
	{
		p++;
	}
	return p - str;

}

int main()
{
	const char *str1 = "abcdef";
	const char *str2 = "abc";
	if (my_strlen1(str1) > my_strlen1(str2))
	{
		printf("str1 > str2\n");
	}
	else
	{
		printf("str1 < str2");
	}
	if (my_strlen2(str1) > my_strlen2(str2))
	{
		printf("str1 > str2\n");
	}
	else
	{
		printf("str1 < str2");
	}
	if (my_strlen3(str1) > my_strlen3(str2))
	{
		printf("str1 > str2\n");
	}
	else
	{
		printf("str1 < str2");
	}
	system("pause");
	return 0;
}

2、strcpy

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

char* my_strcpy(char *destination, const char *source)
{
	char *res = destination;
	assert(destination != NULL);
	assert(source != NULL);
	while ((*destination++ = *source++))
	{
		;
	}
	return res;
}

int main()
{
	char str1[] = "sample string";
	char str2[20];
	char str3[40];
	my_strcpy(str2, str1);
	my_strcpy(str3, "copy successful");
	printf("%s\n", str1);
	printf("%s\n", str2);
	printf("%s\n", str3);
	system("pause");
	return 0;
}

3、strcat

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

char* my_strcat(char *destination, const char *source)
{
	char *res = destination;
	assert(destination);
	assert(source);
	while (*destination)
	{
		destination++;
	}
	while (*destination++ = *source++)
	{
		;
	}
	return res;
}

int main()
{
	char ch[20] = "\0";
	my_strcat(ch, "I ");
	my_strcat(ch, "am ");
	my_strcat(ch, "a ");
	my_strcat(ch, "student!");
	puts(ch);

	putchar("\n");

	char str[80] = "\0";
	my_strcat(str, "these ");
	my_strcat(str, "strings ");
	my_strcat(str, "are ");
	my_strcat(str, "concatenated.");
	puts(str);
	system("pause");
	return 0;
}

4、strstr

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

char* my_strstr(const char *destination, const char *source)
{
	assert(destination);
	assert(source);

	char *pdestination = (char *)destination;
	char *psource = (char *)source;
	char *str = NULL;

	if (*source == '\0')
	{
		return NULL;
	}
	while (*pdestination)
	{
		str = pdestination;
		psource = source;
		while (*str && *psource && (*str == *psource))
		{
			str++;
			psource++;
		}
		if (*psource == '\0')
		{
			return pdestination;
		}
		pdestination++;
	}

}

int main()
{
	char str1[] = "This is a simple string";
	char *pstr;
	pstr = my_strstr(str1, "simple");
	strncpy(pstr, "simple", 6);
	puts(str1);	system("pause");
	return 0;
}

5、strcmp

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

int my_strcmp(const char *destination, const char *source)
{
	int res = 0;
	assert(destination);
	assert(source);
	while (!(res = *(unsigned char *)destination - *(unsigned char *)source) && *source)
	{
		destination++;
		source++;
	}
	if (res < 0)
	{
		res = -1;
	}
	else if (res > 0)
	{
		res = 1;
	}
	return res;
}

int main()
{
	const char *str1 = "abcde";
	const char *str2 = "abcd";
	if (my_strcmp(str1, str2) == 0)
	{
		printf("str1 == str2\n");
	}
	else if (my_strcmp(str1, str2) >= 0)
	{
		printf("str1 >= str2\n");
	}
	else
	{
		printf("str1 <= str2\n");
	}
	system("pause");
	return 0;
}

6、memcpy

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

struct Student
{
	char name[20];
	int age;
}person, person_copy;

void* my_memcpy(void *destination, void *source, size_t num)
{
	void *res = destination;
	assert(destination);
	assert(source);
	while (num--)
	{
		*(char *)destination = *(char *)source;
		destination = (char *)destination + 1;
		source = (char *)source + 1;
	}
	return res;
}

int main()
{
	char myname[] = "Pierre de Fermat";

	/*using memcpy to copy string:*/
	my_memcpy(person.name, myname, sizeof(myname)+1);

	person.age = 23;
	/*using memcpy to copy structure:*/
	my_memcpy(&person_copy, &person, sizeof(person));

	printf("person_copy:%s,%d\n", person_copy.name, person_copy.age);
	system("pause");
	return 0;
}

7、memmove

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

void* my_memmove(void *destination, void *source, size_t num)
{
	void *res = destination;
	assert(destination);
	assert(source);
	if (destination <= source || (char *)destination >= ((char *)source + num))
	{
		while (num--)
		{
			*(char *)destination = *(char *)source;
			destination = (char *)destination + 1;
			source = (char *)source + 1;
		}
	}
	else
	{
		destination = (char *)destination + num - 1;
		source = (char *)source + num - 1;
		while (num--)
		{
			*(char *)destination = *(char *)source;
			destination = (char *)destination - 1;
			source = (char *)source - 1;
		}
	}
	return res;
}

int main()
{
	char str[] = "memmove can be very useful......";
	my_memmove(str + 20, str + 15, 11);
	puts(str);
	system("pause");
	return 0;
}
发布了117 篇原创文章 · 获赞 48 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/gp1330782530/article/details/104477971
今日推荐