模拟实现C语言中字符串函数

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
size_t Strlen(const char* str) {
	int i;
	size_t size = 0;
	assert(str!=NULL);
	for (i=0;str[i]!= '\0';i++) {
		size++;
	}
	return size;
	/*const char* p = str;
	for (;*str != '\0';str++) {

	}
	return str - p;
*/
}
int main() {
	char* a= "abcdef";
	printf("%d\n", Strlen(a));
	system("pause");
	return 0;
} 




#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
char* Strcat(char* dest, const char* src) {
	assert(dest != NULL);
	assert(src != NULL);
	int i = 0;
	for (;dest[i] != '\0';i++);
		for (int j = 0;src[j] != '\0';i++,j++) {
			dest[i] = src[j];
		}
	dest[i] = '\0';
	return dest;
 }
int main() {
	char str1[1024] = "abc";
	char str2[1024] = "def";
	Strcat(str1, str2);
	printf("%s\n", str1);
	printf("%s\n", str2);
	system("pause");
	return 0;
}





#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
 const char* Strstr(const char* str1, const char* str2) {
	assert(str1 != NULL);
	assert(str2 != NULL);
	if (*str2 == '\0') {
		return NULL;
	}
	const char* black_ptr = str1;
	while (*black_ptr != '\0') {
	 const char* red_ptr = black_ptr;
	 const 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[] = "abcdef";
	char str2[] = "bcd";
	const char* ret = Strstr(str1, str2);
	printf("%p\n", str1);
	printf("%p\n", ret);
	system("pause");
	return 0;
}



#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
int Strcmp(char* str1, char*str2) {
	assert(str1 != NULL);
	assert(str2 != NULL);
	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;
		}
	}
	if (str1[i] < str2[i]) {
		return -1;
	}
	else if (str1[i] > str2[i]) {
		return 1;
	}
	else {
		return 0;
	}
}
int main() {
	char str1[1024] = "bbbabc";
	char str2[1024] = "bbb";
	int ret = Strcmp(str1, str2);
	if (ret < 0) {
		printf("str1<str2\n");
	}
	else if (ret > 0) {
		printf("str1>str2\n");
	}
	else if(ret==0){
		printf("str1==str2\n");
	}

	system("pause");
	return 0;
}



#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void* Memcpy(void*dest,const void*src,size_t num) {
	size_t i = 0;
	char* pdest = (char*)dest;
	const char* psrc = (const char*)src;
	for (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", arr2[i]);
	}
	system("pause");
	return 0;
}




#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void* Memmove(void*dest, const void*src, size_t num) {
	size_t i = 0;
	char* pdest = (char*)dest;
	const char* psrc = (const char*)src;
	if (pdest >= psrc && pdest < (psrc + num)) {
		for (i = num;i >0;i--) {
			pdest[i] = psrc[i];
		}

	}
	else {

		for (i = 0;i < num;i++) {
			pdest[i] = psrc[i];
		}
	}
	return dest;
}
int main() {
	int arr1[4] = { 0 };
	int arr2[4] = { 1,2,3,4 };
	Memmove(arr1, arr2, 12);
	for (int i = 0;i < 4;i++) {
		printf("%d\n", arr1[i]);
	}
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值