【C语言】字符串函数的模拟实现--汇总

目录

分类汇总

strlen的模拟实现

 strcpy的模拟实现

strcat的模拟实现

strcmp的模拟实现

strncpy的模拟实现

 strncat的模拟实现

strncmp的模拟实现

strstr的模拟实现

strtok的模拟实现

strerror的使用

memcpy的模拟实现

memmove的模拟实现

memset的模拟实现 

mencmp的模拟实现


分类汇总

求字符串长度
strlen
长度不受限制的字符串函数
strcpy
strcat
strcmp
长度受限制的字符串函数
strncpy
strncat
strncmp
字符串查找
strstr
strtok
错误信息报告
strerror
字符操作,内存操作函数
memcpy
memmove
memset
memcmp

strlen的模拟实现

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <assert.h>
//机器器方法
int my_strlen_1(const char* str)
{
	assert(str);
	int count = 0;
	while (*str++)
	{
		count++;
	}

	return count;
}
//递归的方法
int my_strlen_2(const char* str)
{
	assert(str);
	if (*str)
	{
		return 1+ my_strlen_2(str+1);
	}
	return 0;
}
//指针的方法
int my_strlen_3(const char* str)
{
	assert(str);
	char* temp = str;
	while (*str++)
	{
		;
	}
	return str - temp-1;
}

int main()
{
	char arr[] = "asdlld";
	int ret1 = my_strlen_1(arr);
	int ret2 = my_strlen_2(arr);
	int ret3 = my_strlen_3(arr);

	printf("%d\n%d\n%d\n", ret1,ret2,ret3);

	return 0;
}

 strcpy的模拟实现

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

char* my_strcpy(char* des, const char* sou)
{
	assert(des);
	assert(sou);
	char* ret = des;
	while (*des++ = *sou++)
	{
		;
	}
	return ret;
}

int main()
{
	char arr1[] = "abcdef";
	char arr2[20] = { 0 };

	my_strcpy(arr2, arr1);
	printf("%s", arr2);

	return 0;
}

strcat的模拟实现

char* my_strcat(char* des, const char* sou)
{
	assert(des&&sou);
	char* ret = des;
	while (*des++)
	{
		;
	}
	des--;
	while (*des++=*sou++)
	{
		;
	}
	return ret;
}
int main()
{
	char arr1[] = "abcdef";
	char arr2[30] = "ahsjde";

	my_strcat(arr2, arr1);
	printf("%s", arr2);

	return 0;
}

strcmp的模拟实现

#define _CRT_SECURE_NO_WARNINGS

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

//int my_strcmp(const char* string1, const char* string2)
//{
//	assert(string1&&string1);
//
//	while (*string1 == *string2)
//	{
//		if (*string1=='\0')
//			return 0;
//		string1++;
//		string2++;
//	}
//	if (*string1 > *string2)
//		return 1;
//	else
//		return -1;
//
//}

//化简
int my_strcmp(const char* string1, const char* string2)
{
	assert(string1&&string1);

	while (*string1++ == *string2++)
	{
		if (*string1 == '\0')
			return 0;
	}

		return *string1 - *string2;

}

int main()
{
	char arr1[] = "abcdfe";
	char arr2[] = "abcdcd";

	int ret=my_strcmp(arr1,arr2);
	
	if (ret > 0)
		printf(" > ");
	else if (ret < 0)
		printf(" < ");
	else
		printf(" == ");

	return 0;
}

strncpy的模拟实现

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

char* my_strncpy(char* des, const char* src, size_t count)
{
	assert(des&&src);
	char* ret = des;
	while (count && ((*des++ = *src++) != '\0'))
	{
		count--;
	}
	if (count)
	{
		while (count)
		{
			*des++ = '\0';
		}
	}
	return ret;
}

int main()
{
	char arr1[20] = {0};
	char arr2[] = "abcdcd";

	 my_strncpy(arr1, arr2, 3);

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

	return 0;
}

 strncat的模拟实现

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

char* my_strncat(char* dest,const char* src, size_t count)
{
	assert(dest&&src);
	char *ret = dest;

	while (*dest)
	{
		dest++;
	}
	while (count&&((*dest++ = *src++)!='\0'))
	{
		count--;
	}

	if (count)
	{
		while (count--)
		{
			*dest++ = '\0';
		}
	}

	return ret;
}

int main()
{
	char arr1[20] = "shah";
	char arr2[] = "abcdcd";

	 my_strncat(arr1, arr2, 3);

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

	return 0;
}

strncmp的模拟实现

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

int my_strncmp(const char *str1, const char *str2, size_t count)
{
	assert(str1&&str2);

	while ((*str1 == *str2) && *str1 && (count--))
	{
		str1++;
		str2++;
	}
	if (*str1 < *str2)
		return -1;
	else if (*str1 > *str2)
		return  1;
	else
		return 0;
}
int main()
{
	char *s1 = "abcd";
	char *s2 = "abcc";
	int ret=my_strncmp(s1, s2, 4);

	if (ret > 0)
		printf(">");
	else if (ret < 0)
		printf("<");
	else
		printf("==");
	return 0;
}

strstr的模拟实现

strstr的模拟实现图

 my_strstr的代码实现

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

char* my_strstr(const char* str1, const char* str2)
{
	assert(str1&&str2);
	const char* s1 = str1;
	const char* s2 = str2;
	const char* p = str1;
	
	while (*p)
	{
		s1 = p;  //用来确定找到不同值之后返回的位置
		s2 = str2;
		while (*s1  && *s2  && *s1 == *s2)
		{
			s1++;
			s2++;
		}
		if (*s2 == '\0')
		{
			return (char*)p;//接受类型为char*,p类型由const修饰,返回值强制类型转换为char*
		}
		p++;         //重新确定起始位置     
	}
	return NULL;//没有找到返回空指针
}
 //如果s1==s2进入循环直到不等于,如果s2是等于'\0',那么证明已经s2已经循环完毕且每个是与s1的值相等,所以返回p指针
 //如果进行之后不相等退出循环,指针p++,将新的起始赋予str1,若找到返回指针p,若没有找到继续循环到p指针为‘\0’,返回空指针

int main()
{
	char *s1 = "ssabcdce";
	char *s2 = "abcd";
	char* ret=my_strstr(s1, s2);

	if (ret == NULL)
	{
		printf("子串不存在\n");
	}
	else
	{
		printf("%s\n", ret);
	}

	return 0;
}

strtok的模拟实现

strtok的举例使用

int main()
{
	char arr[] = "zhangsan,lisi,ermazi";
	
	char* ret;
    ret = strtok(arr, ",");
	printf("%s ", ret);

	ret = strtok(NULL, ",");
	printf("%s ", ret);

	ret = strtok(NULL, ",");
	printf("%s ", ret);
	return 0;
}

 my_strtok的实现代码:

char* my_strtok(char* str, const char* delim)
{
	// 生成替换字符表
	char table[256] = { 0 };
	while (*delim != '\0')
	{
		table[*delim] = 1;
		delim++;
	}

	// 使用 static 类型指针保存上一次函数调用时的字符串地址
	static char* pstr = NULL;
	if (str != NULL)
	{
		pstr = str;
	}

	// 保证 pstr 指向以非替换字符为首的子字符串
	while (*pstr != '\0' && table[*pstr] == 1)
	{
		pstr++;
	}

	// ret 保存返回子字符串的首地址
	char* rst = (*pstr != '\0') ? pstr : NULL;

	while (*pstr != '\0')
	{
		if (table[*pstr] == 1)
		{
			// 切割得到子字符串,且 pstr 最后指向子字符串的下一字符
			*pstr++ = '\0';
			break;
		}
		else
		{
			pstr++;
		}
	}

	return rst;
}
int main()
{
	char arr[] = "zhangsan,lisi,ermazi";
	
	char* ret;
    ret = my_strtok(arr, ",");
	printf("%s ", ret);

	ret = my_strtok(NULL, ",");
	printf("%s ", ret);

	ret = my_strtok(NULL, ",");
	printf("%s ", ret);
	return 0;
}

strerror的使用

#include <errno.h>

int main()
{
	printf("%s\n", strerror(0));
	printf("%s\n", strerror(1));
	printf("%s\n", strerror(2));
	printf("%s\n", strerror(3));
	printf("%s\n", strerror(4));
	printf("%s\n", strerror(5));

	//errno - C语言设置的一个全局的错误码存放的变量
	FILE* pf = fopen("C:\\Users\\zpeng\\Desktop\\test.txt", "r");
		if (pf == NULL)
		{
			printf("%s\n", strerror(errno));
			return 1;
		}
		else
		{
			printf("yes!");
		}
		return 0;

}

memcpy的模拟实现

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

void* my_memcpy(void* str1,const void* str2, rsize_t n)
{
	assert(str1&&str2);

	void* ret = str1;

	while (n--)
	{
		*(char*) str1 = *(char*) str2;
		(char*)str1 += 1;
		(char*)str2 += 1;
	}
	return ret;
}

int main()
{
	int arr1[30] = { 0 };
	int arr2[] = { 1, 2, 3, 4 };

	my_memcpy(arr1, arr2,20);

	for (int i = 0; i < 4; i++)
	{
		printf("%d", arr1[i]);
	}
}

memmove的模拟实现

memmove的模拟实现图

 memmove的模拟实现代码

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

void* my_memmove(void* str1,const void* str2, size_t n)
{
	assert(str1&&str2);
	void* ret = str1;

	if (str1 < str2)
	{
		while (n--)
			{
				*(char*) str1 = *(char*) str2;
				(char*)str1 += 1;
				(char*)str2 += 1;
			}
	}
	else
	{
		while (n--)
		{
			*((char*)str1+n) = *((char*)str2+n);
		}
	}
	return ret;
}
int main()
{
	int arr1[] = { 1, 2, 3, 4 };

	my_memmove(arr1, arr1+2,8);

	for (int i = 0; i < 4; i++)
	{
		printf("%d", arr1[i]);
	}	
}

memset的模拟实现 

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

void *my_memset(void *dest,const int  c, rsize_t n)
{
	assert(dest);

	char *pdest = (char *)dest;

	while (n--)
	{
		*pdest++ = c;
	}
	return dest;
}

int main()
{
	char arr1[] = "hello world ";
	int ret=strlen(arr1);

	my_memset(arr1, 1, ret);

	for (int i = 0; i < ret; i++)
	{
		printf("%d", arr1[i]);
	}


}

mencmp的模拟实现


int my_memcmp(const void* str1, const void* str2, size_t count)
{
	assert(str1 && str2);
	const char* s1 = (const char*)str1;
	const char* s2 = (const char*)str2;
	int ret = 0;
	while (count--)
	{
		if ((ret = *s1 - *s2) != 0)
			break;
		s1++;
		s2++;
	}

	return ret;
}
int main()
{
	int a[] = { 1, 2, 3, 4, 5 };
	int b[] = { 1, 2, 4, 3, 5, 6 };
	int ret = my_memcmp(a, b, sizeof(int)* 5);

	if (ret > 0)
		printf(">");
	else if (ret < 0)
		printf("<");
	else
		printf("==");
}

猜你喜欢

转载自blog.csdn.net/includeevey/article/details/125591703