Analog strings and memory functions implemented

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/DX_Jone/article/details/102606157

This article focuses on:

1. string length requirements: strlen
2. unrestricted length string functions: strcpy, strcat, syrcmp
3. limited length string functions: strncpy, strncat, strncmp
4. search string: strstr, strtok
5. Report Error: strerror
6. The memory operation functions: memcpy, memmove, memset, memcmp

In the C language processing string of characters and very frequent, but the C language itself is not of type string, string constants are usually placed in the string or character array. String constant string functions that apply to it without modification.

A simulation on the classification and string functions to achieve:

1.strlen (required string length)

size_t  strlen ( const char * str );

//注意其中size_t表示返回值是一个无符号的长整型;
//其中const 指其指向的内容不能进行修改;

note:

The string has' \ 0 'as an end flag, strlen function returns the string' \ 0 'appear in front of the number of characters (not including \ 0'). Parameter points to the string must be '\ 0' end.
Note that the function return value size_t, is unsigned (error-prone)

Strlen function on analog implementation:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
size_t strlen(const char* str){
	assert(str!=NULL);
	size_t count=0;
	while(str[i]!='\0'){
		count++;
	}
	return count;
}
int main(){
	char str[]="hehe";
	char* p=str;
	if(p!='\0'){
		printf("%lu\n",strlen(p));
	}
	system("pause");
	return 0;
}

2.strcpy (Copy String)
char* strcpy(char * destination, const char * source );
注意:字符串只能使用 = 初始化,不能使用 = 赋值;

Achieved:
(1) translates the source string '\ 0' copied to the target space.
(2) target space must be large enough to ensure that the source string can be stored.
(3) target space must be variable.

Strcpy function on analog implementation:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
char* strcpy(char *dest,char* src){
	assert(dest!=NULL);
	assert(src!=NULL);
	for(int i=0;dest[i]!='\0';i++){
		dest[i]=src[i];
	}
	dest[i]='\0';
	return 0;
}
int main(){
	char dest[]="hehehe";
	strcpy(dest,"haha");
	printf("%s\n",dest);
	system("pause");
	return 0;
}
3.strcat (string concatenation)
char * strcat ( char * destination, const char * source );

Source string must be '\ 0' ends;
the target area must be large enough to accommodate the contents of the source string;
target space must be modified;

Strcat function on analog implementation:


#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
char* strcat(char* dest,char* src){
	assert(dest!=NUL);
	assert(src!=NULL);
	for(int i=0;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 dest[1024]="hehehe";
	strcat(dest,"hahaha");
	printf("%s\n",dest);
	system("pause");
	return 0;
}

4.strcmp (string comparison)
int strcmp ( const char * str1, const char * str2 ); 

Standard:
first string is greater than the second string, it returns a number greater than 0;
first string equals the second string, 0 is returned;
the first string is less than the second string, number less than 0 is returned;

Strcmp function on analog implementation:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int strcmp(const char* str1,const char* str2){
	assert(str1!=NULL);
	assert(str2!=NULL);
	while(*str1!='\0' && *str2!='\0'){
		if(*str1>*str2){
			return 1;
		}
		if(*str1<str2){
			return -1;
		}
		else{
		++str1;
		++str2;
		}
	}
	if(*str1=='\0' && *str2!='\0'){
		return -1;
	}
	else if(*str1!='\0' && *str2=='\0'){
		return 1;
	}
	else if(*str1=='\0' && *str2!='\0'){
	 	return 0;
	 }
}

int main(){
	char str1[]="hahahaha";
	char str2[]="hehehe";
	int ret=strcmp(str1,str2);
	if(ret<0){
		printf("小于\n");
	}else if(ret==0){
		printf("等于\n");
	}else if(ret>0){
		printf("大于\n");
	}
	system("pause");
	return 0;
}
		
5.strstr (string search)
char * strstr ( const char *str1, const char *str2 );

Strstr function on analog implementation:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
char* strstr(const char* str1,const char* str2){
	assert(str1!=NULL);
	assert(str2!=NULL);
	if(*str1=='\0' && *str2=='\0'){
		return NULL;
	}
	const char* black_ptr=str1;
	while(*black_ptr!='\0'){
		const char* sub_ptr=str2;
		const char*red_ptr=black_ptr;
		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;
}
		

Second presentation on memory function and simulation to achieve:

1.memcpy (string copy function)

void * memcpy ( void * destination, const void * source, size_t num );1)这个函数在遇到 '\0' 的时候并不会停下来。
(2)如果source和destination有任何的重叠,复制的结果都是未定义的。

Function:
copy src to the start address for the address n consecutive bytes of data into the address space as a starting point dest address.

Memcpy function on analog implementation:

#include<stdlib.h>
#include<string.h>
#include<assert.h>
void * Memcpy (void* dest ,const void* src, size_t num) {
	assert (dest!= NULL);
    assert (src!=NULL);
    char *pdest= (char* ) dest;
    char * psrc=(char* )src;
    for (size_t=i; i<num;++i) {
         pdest[i]=psrc[i];
    }
    return dest;
 }
int main( ) {
   	int arr1[4] ={1,2,3,4};
   	int arr2[4] ={0};
   	memcpy (arr2, arr1, sizeof(arr1));
   	for ( int i=0; i<4; ++i) {
      	printf ( "%d\n", arr2[i]);
    }
    system("pause");
   	return 0;
}

Guess you like

Origin blog.csdn.net/DX_Jone/article/details/102606157