Common string operation functions and simulation implementation - detailed explanation


Preface

Characters and strings are processed very frequently in C language, but C language itself does not have a string type. Strings are usually placed in constant strings or character arrays. String constants are suitable for string functions that do not modify them.


1. Introduction to common string operation functions:

1.strlen: Find the length of a string

size_t strlen( const char* str );

  • The string has '\0' as the end mark, and 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, which is unsigned (error-prone)
#include <stdio.h>
#include <string.h>
int main()
{
    
    
	char str[] = "abcdef";
	printf("%d", strlen(str));
	//运行结果为6
	return 0;
}

2.strcpy: copy string

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

  • The source string must end with '\0'.
  • The ‘\0’ in the source string will be copied to the target space.
  • The target space must be large enough to accommodate the source string.
  • The target space must be variable.
#include <stdio.h>
#include <string.h>
int main()
{
    
    
	char str1[] = "abcdef";
	char str2[10] = {
    
     0 };
	printf("%s", strcpy(str2, str1));
	//运行结果为abcdef
	return 0;
}

3.strcmp: compare the size of strings

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

  • What is compared is the size of the ASCII code of the character, not the length of the string.
  • 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.
#include <stdio.h>
#include <string.h>
int main()
{
    
    
	char str1[] = "abcdef";
	char str2[] = "abcq";
	printf("%d", strcmp(str1, str2));
	//运行结果为小于0的数字,表示str1小于str2
	return 0;
}

4.strcat: Append string content

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

  • 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.
#include <stdio.h>
#include <string.h>
int main()
{
    
    
	char str1[10] = "abcd";
	char str2[] = "efg";
	printf("%s", strcat(str1, str2));
	//运行结果为abcdefg
	return 0;
}

5.strstr: Determine substring

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

  • Returns a pointer to the first occurrence of str2 in str1, if str2 is not str1.
  • If the sequence of str2 is not found in str1, NULL is returned.
#include <stdio.h>
#include <string.h>
int main()
{
    
    
	char str1[] = "abbbcdef";
	char str2[] = "bbcd";
	printf("%s", strstr(str1, str2));
	//运行结果为bbcdef
	return 0;
}

6.strtok: cut string

char * strtok ( char * str, const char * sep );

  • The sep parameter is a string that defines the set of characters used as separators. The first parameter specifies a string containing zero or more tokens separated by one or more delimiters in the sep string.
  • The strtok function finds the next mark in str, ends it with '\0', and returns the first address of the string on the left side after cutting. When the splitting is completed or cannot be split, NULL is returned; (Note: the strtok function will change the manipulated string, so the string split using 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 token in str, and the strtok function will save its position in the string.
  • The first parameter of the strtok function is NULL, and the function will start at the saved position in the same string and search for the next token. If no more tokens exist in the string, a NULL pointer is returned.
#include <stdio.h>
#include <string.h>
int main()
{
    
    
	char str1[] = "www.csdn.net";
	char buf[20] = {
    
     0 };
	strcpy(buf, str1);//将str1拷贝到buf临时使用
	char sep[] = ".";
	int i = 0;
	for (i = strtok(buf, sep); i != NULL; i = strtok(NULL, sep))
	{
    
    
		printf("%s ", i);//运行结果为www csdn net
	}
	return 0;
}

2. Simulation implementation

1.strlen:

The code is as follows (example):

int my_strlen(const char* p)
{
    
    
	assert(p != NULL);//断言,为空指针就会报错
	int count = 0;
	while (*p++ != 0)
	{
    
    
		count++;
	}
	return count;
}

2.strcpy:

The code is as follows (example):

char* my_strcpy(char* dest, const char* src)
{
    
    
	assert(dest && src);//断言dest和src有没有空指针,有程序就会报错
	char* ret = dest;
	while (*dest++ = *src++)
	{
    
    
		;
	}
	return ret;
}

3.strcmp:

int my_strcmp(char* s1, char* s2)
{
    
    
	assert(s1 && s2);
	while (*s1 && *s2 && *s1 == *s2)
	{
    
    
		s1++;
		s2++;
	}
	if (*s1 && *s2)
	{
    
    	
		return *s1 - *s2;
	}
	else
	{
    
    
		return 0;
	}
}

4.scratched:

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

5.strstr:

char* my_strstr(const char* dest, const char* src)
{
    
    
	assert(dest && src);
	const char* s1 = dest;
	const char* s2 = src;
	const char* cul = dest;
	while (*cul)
	{
    
    
		s1 = cul;
		s2 = src;
		while (*s1 && *s2 && *s1 == *s2)
		{
    
    
			s1++;
			s2++;
		}
		if (*s2 == '\0')
		{
    
    
			return cul;
		}
		cul++;
	}
}

Summarize

The above is what I will talk about today. This article only briefly introduces the use of commonly used string operation functions and the use and precautions of library functions for processing characters and strings, as well as the simulation implementation code.

Guess you like

Origin blog.csdn.net/weixin_61661271/article/details/123777061