C语言自行实现字符串常用库函数

1.实现strcpy
完成字符串的复制

#include
#include
#include
char* Strcpy(char* dest,const char* src) {
	assert(dest != "");
	assert(src != "");
	int i = 0;
	for (i=0; src[i] != '\0'; i++) {
		dest[i] = src[i];
	}
	dest[i] = '\0';
	return dest;
}
int main() {
	char arr1[] = "abcdef";
	char arr2[1024] = "";
	Strcpy(arr2, arr1);
	printf("%s", arr2);
	return 0;
	system("pause");
}

2.实现strcat
完成字符串的拼接

#include
#include
#include
char* Strcat(char* dest, const char* src) {
	assert(dest != "");
	assert(src != "");
	int i = 0;
	for (i = 0; dest[i] != '\0'; i++);
	for (int j = 0; src[j] != '\0'; j++, i++) {
		dest[i] = src[j];
	}
	dest[i] = '\0';
	return dest;
}
int main() {
	char arr1[] = "abcdef";
	char arr2[1024] = "qwert";
	Strcat(arr2, arr1);
	printf("%s", arr2);
	return 0;
	system("pause");
}

3.实现strstr
一个字符串中是否包含另一个字符串

#include
#include
#include
char* Strstr(const char* str1, const char* str2) {
	assert(str1 != "");
	assert(str2 != "");
	if (*str2 == '\0') {
		return NULL;
	}
	//黑指针功能是记录从哪个位置找字符串子串
	char* black_ptr = str1;
	while (*black_ptr != '\0') {
		char* red_ptr = black_ptr;
		char* sub_ptr = str2;
		while (*red_ptr!='\0'
			    &&*sub_ptr!='\0'
			    &&*red_ptr == *sub_ptr) {
			++sub_ptr;
			++red_ptr;

		} 
		if (*sub_ptr == '\0') {
			printf("找到了\n");
			return black_ptr;
		}
		++black_ptr;
	}
	printf("没找到\n");
	return NULL;
}
int main() {
	char str1[] = "abcdef";
	char str2[] = "bcd";
	char* ret=Strstr(str1, str2);
	printf("%p\n", ret);
	return 0;
	system("pause");
}

4.实现strchr
查找字符串s中首次出现字符c的位置。

#include
#include
#include
char* Strstr(const char* str1, const char* str2) {
	assert(str1 != "");
	assert(str2 != "");
	if (*str2 == '\0') {
		return NULL;
	}
	//黑指针功能是记录从哪个位置找字符串子串
	char* black_ptr = str1;
	while (*black_ptr != '\0') {
		char* red_ptr = black_ptr;
		char* sub_ptr = str2;
		while (*red_ptr != '\0'
			&&*sub_ptr != '\0'
			&&*red_ptr == *sub_ptr) {
			++sub_ptr;
			++red_ptr;

		}
		if (*sub_ptr == '\0') {
			return black_ptr;
		}
		++black_ptr;
	}
	return NULL;
}
int main() {
	char str1[] = "abdcbdef";
	char str2[] = "bd";
	char* ret = Strstr(str1, str2);
	printf("%p\n", ret);
	return 0;
	system("pause");
}

5.实现strcmp
完成两个字符串的比较

#include
#include
#include
int Strcmp(const char* str1, const char* str2) {
    assert(str1 != "");
    assert(str2 != "");
	int i = 0;
	for (; str1[i] != '\0'&&str2[i] != '\0'; i++) {
		if (str1[i] < str2[i]) {
			return -1;
		}
		else if (str1[i] > str2[i]) {
			return 1;
		}
		else {}
	}
	if (str1[i] < str2[i]) {
		return -1;
	}
	else if (str1[i] > str2[i]) {
		return 1;
	}
	else {
		return 0;
	}
}
int main() {
	char str1[1024] = "abcde";
	char str2[1024] = "abcde";
	int ret=Strcmp(str1, str2);
	if (ret > 0) {
		printf("str1>str2");
	}
	else if (ret < 0) {
		printf("str1

6.实现memcpy
内存拷贝

#include
#include
#include
void* Memcpy(void* dest, const void* src, size_t num) {
	const char* psrc = (const char*)src;
	char* pdest = (char*)dest;
	assert(dest);
	assert(src);
	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);
	for (int i = 0; i < 4; i++) {
		printf("%d\n", arr1[i]);
	}
	return 0;
	system("pause");
}

7.实现memmove
内存拷贝,但是能够处理空间重合的情况

#include
#include
#include
void* Memcpy(void* dest, const void* src, size_t num) {
	const char* psrc = (const char*)src;
	char* pdest = (char*)dest;
	assert(dest);
	assert(src);
	if (dest >= src && (char*)dest < ((char*)src + num)) {
		for (size_t i = num; i > 1; i--) {
			pdest[i] = psrc[i];
		}
		return dest;
	}
	else {
		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);
	for (int i = 0; i < 4; i++) {
		printf("%d\n", arr1[i]);
	}
	return 0;
	system("pause");
}

你可能感兴趣的:(C语言自行实现字符串常用库函数)