Simulation Implementation of string function and character function (strlen,strcpy... And other library functions)

1.strlen: find string length

Implementation (1): Counter mode

int myStrlen(const char* str) {
	int count = 0;
	while (*str) {
		*str++;
		count++;
	}
	return count;
}

(2) Do not create temporary variable implementation

int myStrlen(const char* str) {
	if (*str == '\0') {
		return 0;
	}
	return 1 + myStrlen(str+1);
}

2. Strcpy (string copy)

char* myStrcpy(char* dest, char* src) {
	assert(dest != NULL);
	assert(src != NULL);
	int i = 0;
	while (src[i] != '\0') {
		dest[i] = src[i];
		i++;
	}
	dest[i] = '\0';
	return dest;
}

3. Strcat (added at the end)

char* myStrcat(char* dest, char* src) {
	assert(dest != NULL);
	assert(src != NULL);
	int destTail = 0;
	while (dest[destTail] != '\0') {
		destTail++;
	}
	int i = 0;
	while (src[i] != '\0') {
		dest[destTail + i] = src[i];
		i++;
	}
	dest[destTail + i] = '\0';
	return dest;
}

 4. Strncat (string tail append function with adjustable length)

char* strncat(char* dest, const char* src, size_t num) {
	assert(dest != NULL);
	assert(src != NULL);
	assert(num != NULL);
	//1. Find the end of dest first
	size_t destTail = 0;
	while (dest[destTail] != '\0') {
		destTail++;
	}
	//When the loop ends, destTail points to the end of dest
	//2. Copy src to the end position s of dest
	size_t i = 0;
	while (src[i] != '\0' && i < num) {
		dest[destTail + i] = src[i];
		i++;
	}
	//When the above cycle ends, it may be
	//1) SRC [i] = > \ 0, you need to set the end of DeST (dest[destTail+1]) to \ 0
	//2)i==num, you also need to add \ 0 to the end of dest;
	dest[ destTail + i] = '\0';
	return dest;
}

5. StrCmp (string comparison size function)

It stipulates that: (1) if the first string is greater than the second string, a number greater than 0 will be returned

(2) the first string is equal to the second string and returns 0;

(3) if the first string is less than the second string, a number less than 0 will be returned

int  myStrcmp(const char* dest,const char* src) {
	assert(dest != NULL);
	assert(src != NULL);
	const char* p1 = dest;
	const char* p2 = src;
	while (*p1 != '\0' && *p2 != '\0') {
		if (*p1 < *p2) {
			return -1;
		}
		else if (*p1 >*p2) {
			return 1;
		}
		else {
			p1++;
			p2++;
		}
	}
	return *p1 - *p2;
}

6. Strncmp (another character is different or num characters are all compared)

//int myStrncmp(const char* str1, const char* str2, size_t num) {
//	assert(str1 != NULL);
//	assert(str2 != NULL);
//	assert(num != 0);
//	size_t  i = 0;
//	while (str1[i] != '\0' && str2[i] != '\0' && i < num) {
//		if (str1[i] < str2[i]) {
//			return -1;
//		}
//		else if (str1[i] > str2[i]) {
//			return 1;
//		}
//		else {
//			i++;
//		}
//	}
//	if (i == num) {
//		return 0;
//	}
//	return str1[i] - str2[i];
//}

7.Strtok} segmentation


char str[] = "aaaa bbbb cccc dddd";
char* pch = strtok(str," ");
while (pch != NULL) {
	printf("%s\n", pch);
	pch = strtok(NULL, "");
}                                           

When calling strtok(str, "") for the first time, start from str (look for "" from the first a). When "" is found, return ~ to the first a, and change the "" to \ 0;

Every subsequent call is strtok(NULL, ""). The first parameter is NULL. Continue to look back from the last end position, find the last letter, encounter, "", and change "" "" to \ 0; Return to the position of the first letter at the same time;

Finally, the first parameter is still NULL. Continue to look for things behind the last position from the last end position ~ end

Disadvantages of this function:

1. Multiple calls are required to complete the function ~ it is complex to use

2. During multiple calls, the parameters are different~

3. The original string will be destroyed during the call

4.strtok uses static variables internally to record the location of the last call;

C + + has boost::split to replace it;

8. Str (string lookup function)

const char* myStrstr(const char* str1, const char* str2) {

     assert(str1 != NULL);

     assert(str2 != NULL);

     const char* black = str1;

      while (*black != '\0') {

              const char* red = black;

              const char* sub = str2;

             while (*red != '\0' && *sub != '\0' && *red == *sub) {

                      red++;

                      sub++;

}
if (*sub == '\0') {

return black;

}

black++;

}

return NULL;

}

9. Mencpy (copy the first few characters and operate the characters)

The function copies num bytes of data backward from the src location to the dest memory location, and the function will not stop when it encounters' \ 0 ';

mencpy Copy
void* myMencpy(void* dest, const void* src, size_t num) {
	assert(dest != NULL);
	assert(src != NULL);
	assert(num != 0);
	char* cdest = (char*)dest;
	const char* csrc = (const char*)src;
	for (size_t i = 0; i < num; i++) {
		cdest[i] = csrc[i];
	}
	return dest;
}

10. Memmove (the only difference between memmove and mencpy is that it can handle memory overlap)

void* myMemmove(void* dest, const void* src, size_t num) {
	assert(dest != NULL);
	assert(src != NULL);
	assert(num != 0);
	char* cdest = (char*)dest;
	const char* csrc = (const char*)src;
	if (csrc <= cdest && cdest <= csrc + num) {
		//If the memory overlaps, the copy is reversed
		for (size_t i = num; i > 0; i--) {
			cdest[i - 1] = csrc[i - 1];
		}
		return dest;
	}
	else{ //If the memory does not overlap, the copy is in progress
		for (size_t i = 0; i < num; i++) {
			cdest[i] = csrc[i];
		}
		return dest;
	}
} 

11. Memcmp (StrCmp with limited length) compares num characters starting from ptr1 and ptr2

int myMemcmp(const void* ptr1, const void* ptr2, size_t num) {
	assert(ptr1 != NULL);
	assert(ptr2 != NULL);
	assert(num != 0);
	const char* cptr1 = (const char*)ptr1;
	const char* cptr2 = (const char*)ptr2;
	for (size_t i = 0; i < num; i++) {
		if (cptr1[i] < cptr2[i]) {
			return -1;
		}
		else if (cptr1[i] > cptr2[i]) {
			return 1;
		}
		else {
			continue;
		}
	}return 0;
}

 12. Memset (initialization pointer size and value of each element)

memset
void* myMemset(void* ptr, int value, size_t num) {
	assert(ptr != NULL);
	assert(num != 0);
	char* cptr = (char*)ptr;
	for (size_t i = 0; i < num; i++) {
		cptr[i] = (char)value;
	}return ptr;
}

Added by qadeer_ahmad on Wed, 29 Dec 2021 10:55:16 +0200