String functions (strlen,strcpy,strcat,strcmp,strstr,strtok)

Posted by jana on Sat, 26 Feb 2022 06:00:27 +0100

catalogue

I strlen - string length

1. Function introduction

2. Simulation Implementation

II strcpy - string copy

1. Function introduction

2. Simulation Implementation

3. strncpy 

III strcat - string append

1. Function introduction

2. Simulation Implementation

 3.strncat

IV strcmp - string comparison

1. Function introduction

2. Simulation Implementation

3.strncmp

V STR - find substring

1. Function introduction

2. Simulation Implementation

Vi strtok - string split

1. Function introduction

2. Function usage

I strlen - string length

1. Function introduction

size_t strlen ( const char * str );

The strlen function is used to find the length of a string. What strlen does is a counter. It starts scanning from a certain position in memory (which can be the beginning of a string, a certain position in the middle, or even an uncertain memory area until it encounters the first string terminator '\ 0', and then returns the counter value (the length does not contain '\ 0')

a key:

  • The string has' \ 0 'as the end flag. The strlen function returns the number of characters that appear before' \ 0 'in the string (excluding' \ 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 (error prone)
//Small test
#include <stdio.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; }

 

2. Simulation Implementation

size_t my_strlen1(char* str) //Counter method
{
	assert(str);
	size_t count = 0;
	while (*str)
	{
		count++;
		str++;
	}

	return count;
}

size_t my_strlen2(char* str) //Pointer pointer method
{
	assert(str);
	char* temp = str;
	
	while (*temp)
	{
		temp++;
	}

	return temp - str;
}

size_t my_strlen3(char* str) //recursion
{
	assert(str);
	if (*str != '\0')
	{
		return 1 + my_strlen3(++str);
	}
	else
	{
		return 0;
	}

}

II strcpy - string copy

1. Function introduction

char* strcpy(char * destination, const char * source );

The strcpy function copies a string into another string space, and the contents of the destination space will be overwritten. Both parameters are pointers, which copy the contents of source to dest destination space, and the return value is the address of destination space, i.e. dest

a key:

  • The source string must end with '\ 0'
  • 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

 

2. Simulation Implementation

char* my_strcpy(char* dest, const char* source)
{
	assert(dest && source); //Neither pointer can be null
	char* ret = dest;       //Used to return the copied address
	while (*dest++ = *source++)  //source = '\ 0' is the end condition
	{
		;
	}

	return ret;
}

3. strncpy 

char * strncpy ( char * destination, const char * source, size_t num );

The difference from the above function is that there is an additional num, which can control the number of characters you want to copy.

a key:

  • 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 the destination until num characters

III strcat - string append

1. Function introduction

char * strcat ( char * destination, const char * source );

strcat function appends the string content pointed to by source to the string pointed to by DeST, and returns the appended string, that is, dest

a key:

  • 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.

2. Simulation Implementation

char* my_strcat(char* dest, const char* source)
{
	assert(dest && source); //Two pointers cannot be null
	char* ret = dest;  //Record the original address for returning

    //Found '\ 0'
	while (*dest)
	{
		dest++;
	}

    //Add
	while (*dest++ = *source++)
	{
		;
	}

	return ret;
}

 

 3.strncat

char * strncat ( char * destination, const char * source, size_t num );

The difference from the above function is that there is an additional num, which can control the number of characters you want to add.

a key:

  • Append num characters from the source string to the destination space.
  • If the length of the source string is less than num, after appending the source string, append 0 after the target until num

IV strcmp - string comparison

1. Function introduction

int strcmp ( const char * str1, const char * str2 );

The strcmp function is a function used to compare the contents of two strings. Its parameters are two pointers pointing to two compared strings. Its return value is a number. When str1 is greater than str2, it returns a number greater than 0; Returns 0 when str1 equals str2; When str1 is less than str2, a number less than 0 is returned.

a key:

  • The string compares not the size of the string length, but the ASCII value of the corresponding position character in the two strings.

 

2. Simulation Implementation

int my_strcmp(const char* str1, const char* str2)
{
	assert(str1 && str2); //The comparison string cannot be empty

	while (*str1 == *str2)
	{
		if (*str1 == '\0')//Compared to the end and all are '\ 0'
			return 0;

		str1++;  //Go straight back for comparison
		str2++;
	}

	return str1 - str2; //The ASCII code of two characters is subtracted
}

 

3.strncmp

int strncmp ( const char * str1, const char * str2, size_t num );

strncmp can control the number of characters you want to compare

For example:

#include<stdio.h>
#include<string.h>
int main()
{
	char arr1[] = "123456";
	char arr2[] = "123467";
	int ret1 = strncmp(arr1, arr2, 4);  // 0 
	int ret2 = strncmp(arr1, arr2, 5);  //Return - 1 
	return 0;
}

V STR - find substring

1. Function introduction

char * strstr ( const char * str1, const char * str2 );

The strstr function looks up the substring str2 in the string str1. If the search is successful, it returns the first element address of the substring str1. If the search fails, it returns NULL.

2. Simulation Implementation

char* my_strstr(const char* str1, const char* str2)
{
	assert(str1 && str2); //Neither string is empty

	char* str = str1;
	char* s1;
	char* s2;

	if (*str2 == '\0') //If the substring is empty, str1 is returned
		return str;

	while (*str) //It is equivalent to trying from str1 one character at a time
	{
		s1 = str;
		s2 = str2;
		while (s1 && s2 && *s1 == *s2) //Only when two characters are equal can we go backward
		{
			s1++;
			s2++;
		}

		if (s2 == '\0') //It is equivalent to going to the end of str2 and finding the substring
			return str;

		str++;
	}

	return NULL;  //All cases in str1 were tried, but no substring was found
}

Vi strtok - string split

1. Function introduction

char * strtok ( char * str, const char * sep );
  • The sep parameter is a string that defines the set of characters used as delimiters
  • The string sep contains one or more delimiters specified by the first parameter sep.
  • 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. (static variable)
  • A NULL pointer is returned if there are no more tags in the string.

 

2. Function usage

//Use it yourself

#include <stdio.h>
int main()
{
 char *p = "GSX.MIUI@XiaoMI.com";
 const char* sep = ".@";
 char arr[30];
 char *str = NULL;
 strcpy(arr, p);    //Copy the data and process the contents of the arr array
 for(str=strtok(arr, sep); str != NULL; str=strtok(NULL, sep))
 {
    printf("%s\n", str);  
 }

}

Topics: C C++