c language string function and memory function

Fill in the notes for a holiday.
Maybe more than ten or twenty articles will be published this month.

What is a string

   "hello world.\n"
  • This string of characters enclosed in double quotation marks is called string literal, or string for short.

The processing of characters and strings in C language is very frequent, but C language itself has no string type. Strings are usually placed in constant strings or character arrays.
String constants apply to string functions that do not modify it.

Note: the end flag of the string is an escape character of \ 0. When calculating the length of a string, \ 0 is the end flag and is not counted as the content of the string.

There will be wonderful events without an end sign

Understanding of '\ 0'

  • '\ 0' does not belong to the content of the string, but belongs to the end separator of the string.
  • The literal value is 0, accounting for one byte.

'\ 0' and '0' are different, '0' is the character 0 and the ASCII value is 48.

strlen

size_t strlen ( const char * str );

Standard reference of strlen

  • The end of the string already contains' \ 0 '. strlen returns the number of characters before' \ 0 '

  • The string pointed to by the parameter must end with '\ 0'

  • Note that the return value of the function is size_t is unsigned, including the calculation is also unsigned (error prone)

    #include<stdio.h>
    #include<string.h>
    int main()
    {
     const char*str1 = "abcdef";
     const char*str2 = "bbb";
     if(strlen(str2)-strlen(str1)>0)
     {
     printf("str2>str1\n");
     } 
     else
     {
     printf("srt1>str2\n");
     }
     return 0;
    }
    //The answer is STR2 > STR1 because the calculated value is also unsigned
    

    Simulation Implementation:

    //Analog implementation, strlen, ordinary counter method
    size_t my_strlen(char* arr)
    {
    	size_t count = 0;
    	while (*arr)
    	{
    		count++;
    		arr++;
    	}
    	return count;
    }
    //Recursive implementation
    size_t my_strlen(char* arr)
    {
    	if (!(*arr))
    	{
    		return 0;
    	}
    	else
    	{
    		return 1 + my_strlen(arr + 1);
    	}
    }
    //Pointer subtraction
    size_t my_strlen(char* arr)
    {
    	char* ret = arr;
    	while (*ret++)
    	{
    		;
    	}
    	return (ret - arr-1);
    
    }
    

    strcpy

    char* strcpy(char * destination, const char * source );
    
  • The source string must end with '\ 0'.

  • Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).

  • The '\ 0' in the source string will be copied to the destination space. The destination space must be large enough to hold the source string.

  • The target space must be variable.

Simulation Implementation:

char *my_strcpy(char *dest, const char*src)  
{  
  char *ret = dest;  
  assert(dest && src );  
  while((*dest++ = *src++))  
  {  
    ;  
  }  
  return ret;  
}

strcat

char * strcat ( char * destination, const char * source );
  • Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.

  • The source string must end with '\ 0'. The target space must be large enough to accommodate the contents of the source string.

  • The target space must be modifiable.

  • How about appending the string to yourself? There will be a dead cycle.

Simulation Implementation:

char* my_strcat(char* Dis, char* Sor)
{
	char* ret = Dis;
	assert(Dis && Sor);
	while (*Dis)
	{
		Dis++;
	}
	while (*Dis++ = *Sor++)
	{
		;
	}
	return ret;
}

strcmp

int strcmp ( const char * str1, const char * str2 );
  • This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.

  • Standard provisions

    • If the first string is greater than the second string, a number greater than 0 is returned
    • If the first string is equal to the second string, 0 is returned
    • If the first string is less than the second string, a number less than 0 is returned

    Simulation Implementation:

    int my_strcmp(const char* arr1, const char* arr2)
    {
      assert(arr1 && arr2)
    	while (*arr1 && *arr2 && *arr1 == *arr2)
    	{
    		arr1++;
    		arr2++;
    	}
    	return *arr1-*arr2;
     }
    

strncpy

char * strncpy ( char * destination, const char * source, size_t num );
  • Copies the first num characters of source to destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it.

  • Copy num characters from the source string to the destination space.

  • If the length of the source string is less than num, after copying the source string, append 0 to num after the destination.

strncat

  • Appends the first num characters of source to destination, plus a terminating null-character.
  • If the length of the C string in source is less than num, only the content up to the terminating null-character is copied

Is to append num characters of string 2 after string 1

  • If the length of n is greater than the second string, it will be appended to \ 0 and will not be appended
  • An additional \ 0 will be appended at the end
char * mystrncat(char * dst, const char * src, size_t n) 
{
  char * tmp = dst;
  while (*dst)
  {
    dst++;
  }
   
  int i;
  for (i = 0; src[i] && i < n; i++)
  {
    dst[i] = src[i];
  }
   
  dst[i] = 0;
  return tmp;
}

strstr

char * strstr ( const char *str1, const char * str2);
  • Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1

Find string 2 in string 1 and return the address at the beginning of string 1 after finding it

NULL if not found

If str2 is an empty string, the first address of str1 is returned directly

strtok

char * strtok ( char * str, const char * sep );
  • The sep parameter is a string that defines the set of characters used as separators, such as "@." It is with '@' and '.' Find and segment
  • The first parameter specifies a string that contains 0 or more tags separated by one or more separators in the sep string
  • The strtok function finds the next tag in str, ends it with \ 0, and returns a pointer to this tag. (Note: the strtok function will change the string to be manipulated, so the string segmented by the strtok function is generally a temporary copy of the content and can be modified.)
  • The first parameter of the strtok function is not NULL. The function will find the first tag in str, and the strtok function will save its position in the string.
  • The first parameter of strtok function is NULL. The function will start at the position saved in the same string to find the next tag.
  • A NULL pointer is returned if there are no more tags in the string
int main()
{
	char str[] ="- This, a sample string.";  
    char * pch;  
    printf ("Splitting string \"%s\" into tokens:\n",str);  
    pch = strtok (str," ,.-");  
    while (pch != NULL)  
    {  
      printf ("%s\n",pch);  
      pch = strtok (NULL, " ,.-");  
    }
	return 0;
}

strerror

char * strerror ( int errnum );

Print error message (hardly used at this stage)

use

printf("%s\n",strerror(errno));
//Or use it directly
perror("fopen");

memset

void *memset( void *dest, int c, size_t count );
  • dest is the variable to change
  • c is the value to be changed
  • count is the number of bytes to change

memcpy

void * memcpy ( void * destination, const void * source, size_t num );
  • The function memcpy copies num bytes of data back to the memory location of destination from the location of source.
  • This function will not stop when it encounters' \ 0 '.
  • If there is any overlap between source and destination, the copied result is undefined.

Simulation Implementation:

void * memcpy ( void * dst, const void * src, size_t count)
{  
  void * ret = dst;  
  assert(dst&&src);  
  while (count--) {  
    *(char *)dst = *(char *)src;  
    dst = (char *)dst + 1;  
    src = (char *)src + 1;  
  }  
  return(ret);  
}

memmove

void* memove(void* destination, const void* source, size_t num)
  • The difference from memcpy is that the source memory block and target memory block processed by memmove function can overlap.
  • If the source space and target space overlap, you have to use the memmove function.

Simulation Implementation:

void * memmove ( void * dst, const void * src, size_t count)  
{  
  void * ret = dst; 
//Front - > rear
  if (dst < src) {  
    while (count--) {  
      *(char *)dst = *(char *)src;  
      dst = (char *)dst + 1;  
      src = (char *)src + 1;  
    }  
  } 
//Front - > rear
  else { 
    while (count--) {  
      *(*(char *)dst+count-1) = *((*(char *)src+count-1);  
    }  
  }
  return ret;
}

memcmp

int memcmp ( const void * ptr1, 
 const void * ptr2, 
 size_t num );
  • Compare num bytes starting from ptr1 and ptr2 pointers

  • The return values are as follows

perror

It will automatically help you type the error message on the screen

You can also pass him a string. He will print the string before the error message and add it in the middle.

Keywords: C Back-end

Added by acrayne on Sun, 20 Feb 2022 02:33:56 +0200