仿寫C語言string.h頭文件檢驗字符串函數

c語言string.h頭文件字符串檢驗函數仿寫

所需頭文件:stdio.h ,stdlib.h, string.h

strlen字符串求長度

int strlen_my(const char* src_str)
{
 	if (NULL == src_str)
	{
		return -1;
	}
	const char* p = src_str;
	while (*p != '\0')
	{	
		p++;
	}
	return p - &(src_str[0]);
}
//遞歸
int strlen_my2(const char* src_str)
{
	if (src_str == NULL)
		return -1;
	if (*src_str)
	{
		return strlen_my2(src_str + 1) + 1;
	}
	else
	{
		return 0;
	}
}
 int main()
{
	const char* p = "hello";
	printf("%d \n",strlen_my(p));
	return 0;
}

strcmp / strncmp字符串比較

int strcmp_my(const char* str_a, const char* str_b)
{
	while ((*str_a != '\0' || *str_b != '\0') && *str_a == *str_b )
	{
		str_a++;
		str_b++;
	}
 	return *str_a - *str_b > 0 ? 1 : (*str_a - * str_b < 0 ? -1 : 0);
}
int strcmp_my2(const char* str_a, const char* str_b)
{
	int sub = 0;
	while ((sub = *str_a - *str_b) == 0 && *str_a++ && *str_b++);	//先賦值,指針再++
 	return sub > 0 ? 1 : (sub < 0 ? -1 : 0);
}
 int strncmp_my(const char* str_a, const char* str_b, size_t front_len)
{
	while (--front_len && *str_a != '\0' && *str_b != '\0' 
		&& *str_a == *str_b)
	{
		str_a++;
		str_b++;
	}
 	return *str_a - *str_b > 0 ? 1 : (*str_a - *str_b < 0 ? -1 : 0);
}
 int main()
{
	const char* p = "hella";
	const char* q = "hell";
	printf("%d \n", strncmp_my(p, q, 4));
	return 0;
}

strchr / strrchr 字符串中查找字符ch

第一個出現的字符ch

//strchr 返回字符串中第一個出現的字符ch
char* strchr_my(const char* src_str, char ch)
{
	if (src_str == NULL)
	{
		return NULL;
	}
 	while (*src_str != '\0' && *src_str != ch)
	{
		src_str++;
	}
 	return *src_str == '\0' ? NULL : src_str;
}
 int main()
{
	const char* p = "hello";
	p = strchr_my(p, 'e');
	if (p == NULL)
		return 0;
	printf("%c\n", *p);
}
 

最後一個出現的字符ch

//strrchr查找字符串中最後一個出現的字母ch
char* strrchr_my(const char* src_str, char ch)
{
	if (NULL == src_str) return NULL;
	const char* p = src_str + strlen(src_str) - 1;
 	while ((p - src_str) >= 0 && *p != ch)
	{
		p--;
	}
	if (p - src_str == -1)
	{
		return NULL;
	}
	return p;
}
 int main()
{
	const char* p = "hello";
	//p = strchr_my(p, 'q');
 	p = strrchr_my(p, 'l');
	if (p == NULL)
		return 0;
	printf("%c\n", *p);
}
 

strstr 字符串匹配:src_str中查找des_str,返回匹配的起始位置,沒有為NULL(BF算法)

char* strstr_my(const char* src_str, const char* des_str)
{
	if (NULL == src_str || NULL == des_str)
	{
		return NULL;
	}
 	int len_src = strlen(src_str);
	int len_des = strlen(des_str);
	int i = 0;	//src_str
	int j = 0;	//des_str
 	while (i < len_src && j < len_des)
	{
		if (src_str[i] == des_str[j])
		{
			i++;
			j++;
		}
		else
		{
			i = i - j + 1;
			j = 0;
		}
	}
 	if (j == len_des)
	{
		return src_str + i - j;
	}
	return NULL;
}
 int main()
{
	const char* p = "hello";
	const char* q = "llo";
	const char* rt = strstr_my(p, q);
	if (rt != NULL)
	{
		printf("%s\n", rt);
		return 0;
	}
	return 0;
}

strcpy / strncpy字符串拷貝

char* strcpy_my(char* des_str, const char* src_str)
{
	if (des_str == NULL || src_str == NULL)
		return NULL;
	char* p = des_str;
	while (*src_str != '\0')
	{
		*p++ = *src_str++;
	}
	*p = '\0';
	return des_str;
}
 char* strncpy_my(char* des_str, const char* src_str, size_t len)
{
	if (des_str == NULL || src_str == NULL || len <= 0)
		return NULL;
	char* p = des_str;
	//源字符串實際長度
	int cur_len = strlen(src_str) <= len ? len : strlen(src_str);
 	while (cur_len--)
	{
		*p++ = *src_str++;
	}
	*p = '\0';
	return des_str;
}
 int main()
{
	const char* p = "hello";
	char q[10] = {0};
 	if (strncpy_my(q, p, 1) != NULL)
	{
		printf("%s\n", q);
	}
 	return 0;
}

strcat / strncat字符串的粘貼

char* strcat_my(char* des_str, const char* src_str)
{
	if (des_str == NULL || src_str == NULL)
		return NULL;
	int len_des = strlen(des_str);
	char* p = des_str + len_des;
 	while (*src_str != '\0')
	{
		*p++ = *src_str++;
	}
	return des_str;
}
 char* strncat_my(char* des_str, const char* src_str, size_t len)
{
	if (des_str == NULL || src_str == NULL || len < 0)
		return NULL;
	int len_des = strlen(des_str);
	char* p = des_str + len_des;
 
	while (len--)
	{
		*p++ = *src_str++;
	}
	return des_str;
}
 int main()
{
	char p[20] = "hello";
	const char* q = "world";
	printf("p = %s\n", p);
	printf("q = %s\n", q);
	if (strncat_my(p, q, 3) != NULL) 
	{
		printf("p = %s\n", p);
	}
	printf("q = %s\n", q);
	return 0;
}

strdup 字符串申請堆區空間存放字符串的副本

char* strdup_my(const char* src_str)
{
	if (NULL == src_str)
		return NULL;
	char* newspace = (char*)malloc((strlen(src_str) + 1 ) * sizeof(char));
	if (newspace == NULL)
		return NULL;
	int i = 0;
	while (*src_str != '\0')
	{
		newspace[i++] = *src_str++;
	}
	newspace[i] = '\0';
	return newspace;
}
int main()
{
	const char* p = "hello";
	char* q = strdup_my(p);
	if (q != NULL)
	{
		printf("%s\n", q);
		free(q);
		q == NULL;
	}
	return 0;
}

總結

本篇文章就到這裡瞭,希望能夠給你帶來幫助,也希望您能夠多多關註WalkonNet的更多內容!

推薦閱讀: