天天看點

(C語言)模拟實作strcpy,strcat,strstr,strchr, strcmp, memcpy, memmove.

1.模拟實作strcpy     char *strcpy( char *strDestination, const char *strSource );

把strSource 拷貝到strDestination,傳回strDestination

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char *Copystr(char *strD, const char *strS)
{
	char *p = strD;
	while (*strD++ = *strS++){
	}
	return p;
	/*char *p = strD;
	while (*strS != '\0'){
		*strD = *strS;
		strD++;
		strS++;
	}
	*strD = '\0';
	return p;*/
}
int main()
{
	char str1[20] = "ABCDEFG";
	char *str2 = "HELLO";
	printf("%s\n", Copystr(str1, str2));
	system("pause");
	return 0;
}
           

2.模拟實作strcat     char *strcat(char *strDestination, const char *strSource);

把strSource 連接配接到strDestination,傳回strDestination

#include<stdio.h>
#include<stdlib.h>
char *Mystrcat(char *strDestination, const char *strSource)
{
	char *p = strDestination;
	while ('\0' != *strDestination){
		strDestination++;
	}
	while (*strDestination++ = *strSource++){
	}
	return p;
}
int main()
{
	char strD[20] = "HELLO ";
	char *strS = "WORLD!";
	printf("%s\n", Mystrcat(strD, strS));
	system("pause");
	return 0;
}
           

3.char *strstr( const char *string, const char *strCharSet );

在string檢視是否有字元串strCharSet,有則從首次發現strCharSet處傳回string,沒有傳回NULL,

若strCharSet長度為0,傳回string

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include <assert.h>
const char* My_strstr(const char *str, const char *strc)
{
	assert(str != NULL);
	assert(strc != NULL);
	const char *s1 = NULL;
	const char *s2 = NULL;
	const char *start = str;
	while (*start != '\0'){
		s1 = start;//s1回退到上一次的下一個位置
		s2 = strc;//s2回退到str的起始位置
	    while (*s1 != '\0' && *s2 != '\0' && *s1 == *s2){
			s1++;
			s2++;
	   }
	   if (*s1 == '\0'){
	   return NULL;
	   }
	   if (*s2 == '\0'){
	   return start;
	   }
	   start++;
	}
	return NULL;

	//不完善
	/*while (*str != '\0'){
		const char *p = str;
		while (*str != '\0' && *strc != '\0' && *str == *strc){
			str++;
			strc++;
		}
		if (*str == '\0'){
			return NULL;
		}
		if (*strc == '\0'){
			return p;
		}
		str++;
	}*/
	
}
int main()
{
    const char* str1 = "ABCCDEFG";
	const char* str2 = "CD";
	printf("%s\n", My_strstr(str1, str2));
	system("pause");
	return 0;
}

           

4.char *strchr( const char *string, int c );

從string左邊開始查找字元c,找到則傳回c首次出現的位置,沒找到傳回NULL 

#include<stdlib.h>
#include<stdio.h>
#include<assert.h>
const char *My_strchr(const char *str,char *c)
{
	assert(str != NULL);
	while (*str != '\0'){
		if (*str == *c){
			return str;
		}
		str++;
	}
	return NULL;
}
int main()
{
	/*char str[] = "ABCDEFG";*/
	char *str = "ABCDEFG";
	char *c = "D";
	printf("%s\n", My_strchr(str, c));
	system("pause");
	return 0;
}
           

5.int strcmp( const char *string1, const char *string2 );

比較兩個字元串大小,string1大傳回>0,string1小于string2傳回<0,相等傳回0 

#include<stdlib.h>
#include<stdio.h>
#include<assert.h>
int My_strcmp(const char* str, const char *strc)
{
	assert(str != NULL && NULL != strc);
	while (*str == *strc){
		if (*str == '\0'){
			return 0;
		}
		str++;
		strc++;
	}
	if (*str > *strc){
		return 1;
	}
	else {
		return -1;
	}
	
	//while (*str != '\0' || '\0' != *strc){
	//	if (*str > *strc){
	//		return 1;
	//	}
	//	else{
	//		return -1;
	//	}
	//	str++;
	//	strc++;
	//}
	//if (*str == '\0' && '\0' == *strc){
	//	return 0;
	//}
}
int main()
{
	const char *str = "ABCDEFG";
	const char *strc = "ABCDG";
	printf("%d\n", My_strcmp("abcd", "abcd"));
	printf("%d\n", My_strcmp("abed", "abcd"));
	printf("%d\n", My_strcmp("abcd", "abed"));
	system("pause");
	return 0;
}
           

6.void *memcpy( void *dest, const void *src, size_t count );

從src的起始位置拷貝count個位元組目标到dest的位置中,傳回指向dest的指針 

#include<stdlib.h>
#include<stdio.h>
#include<assert.h>
void *My_memcpy(char* str,const char *strc, int n)
{
	assert(str != NULL && strc != NULL);
	while (n--){
		*(char *)str++ = *(char *)strc++;
	}
	//for (int i = 0; i < n; i++){
	//	*(str + i) = *(strc + i);
	//}
	return str;
}
int main()
{
	char str1[] = "ABCDEFG";
	const char* str2 = "HELLO WORLD!";
	int arr1[20] = { 1, 2, 3, 4, 5, 6, 7 };
	int arr2[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
	My_memcpy(arr1, arr2, 16);
	My_memcpy(str1, str2, 7);
	printf("%s\n",str1);
	system("pause");
	return 0;
}
           

7.void *memmove( void *dest, const void *src, size_t count );

從src的起始位置移動count個位元組目标到dest的位置中,傳回指向dest的指針,保證正确性 

#include<stdlib.h>
#include<stdio.h>
#include<assert.h>
void *My_memmove(char *dest, const char* src, int count)
{
	assert(dest != NULL && src != NULL);
	void *p = dest;
	if (dest < src){
		while (count--){
			*(char *)dest++ = *(char *)src++;
		}
	}
	else{
		while (count--){
			*((char *)dest+count) = *((char *)src+count);
		}
	}
	return p;
	//char *pd = dest;
	//const char *ps = src;
	///*if (pd <= ps && pd >= ps + count){*///記憶體不沖突
	//if (pd > ps){
	//	while (count--){
	//		*pd++ = *ps++;
	//	}
	//}
	//else{
	//	while (count--){
	//		*(pd + count) = *(ps + count);
	//	}
	//}
	//return dest;
}
int main()
{
	int arr[10] = { 1, 2, 3, 4, 5, 6, 7 };
	//My_memmove(arr, arr + 2, 16);
	My_memmove(arr+2, arr, 16);

	/*char str1[] = "ABCDEFGdwidh";
	const char *str2 = "Attitude to life!";
	int n = 9;
	My_memmove(str1, str2, n);
	printf("%s\n", str1);*/
	system("pause");
	return 0;
}