[C Language] In-depth lecture on the use and optimization of string processing functions

Table of contents

1. String processing functions

1. Measure the length of the string strlen

2. String copy function strcpy/strncpy (copy the first n characters)

 3. String append function strcat/strncat

4. String comparison function strcmp

 5. Character lookup function strchr/strrchr

 6. String search strstr

7. String replacement function memset

8. String cutting strtok

 2. String conversion value

1. Function realization

2. Code ImplementationEdit


1. String processing functions

The functions starting         with str are all string operation functions, and all of them encounter '\0' to end the operation

Header file: #include<string.h>        

1. Measure the length of the string str len

Usage: size_t strlen(const char *s);//const read-only

s is the address of the first element of the string to be measured , '\0' will not be measured

Example:

char str[100]="hello world";

strlen(str) == 11;

2. String copy function strcpy/strncpy (copy the first n characters)

        Copy the string pointed to by src to the memory pointed to by the dest pointer, and copy when encountering '\0' , and the return value is the address of the destination memory.

Usage: char *strcpy(char *dest, const char *src);

           char *strncpy(char *dest, const char *src, size_t n);

dest: destination space address

src: the address of the first element of the original string

Example:

 3. String append function strcat/strncat

Usage: char *strcat(char *dest, const char *src);

           char *strncat(char *dest, const char *src, size_t n);

Append the string pointed to by src to the end of the string pointed to by dest

Example: 

4. String comparison function strcmp

Usage: int strcmp(const char *s1, const char *s2);

           int strncmp(const char *s1, const char *s2, size_t n);

Compare character ASCII code values ​​one by one.

return value:

>0 string s1 > string s2

<0 string s1 < string s2

==0 String s1 == String s2

Example: 

 5. Character lookup function strchr/strrchr

Usage: char *strchr(const char *s, int c);//check from front to back

           char *strrchr(const char *s, int c);//check from back to front

The return value of strchr is to find the address of the first occurrence of character c from front to back , if not found , it returns NULL

 6. String search strstr

用法:char *strstr(const char *haystack, const char *needle);

Return value : Found to return the found address failed to return NULL

7. String replacement function memset

用法:void *memset(void *str, int c, size_t n)

Copies the character  c (an unsigned char) to  the first n  characters  of the string pointed to by  the parameter str .

The value returns a pointer to the memory area str .

Case: replace world with $$$$$

8. String cutting strtok

用法:char *strtok(char *str, const char * delim);

The first cut: str must point to the address of the first element of the string to be cut , delim points to the separator "|"

    Subsequent cutting: str passes NULL delim to point to the separator "|"

Return value: success : the address of the first element of the return value substring

               Failure : return NULL

Cutting method 1:

#include <stdio.h>
#include<string.h>
void test()
{   
	char str[] = "aaaa|bbbb|cccc|dddd";
	int i = 0;
	char *buf[30];//存放字串首元素地址
	buf[i]=strtok(str, "|");//第一次存
	while (buf[i] != NULL)
	{
		i++;
		buf[i] = strtok(NULL, "|");//后续存
	}
    //遍历切割完后的字串
	for (int j = 0; j < i; i++)
	{
		printf("%s\n", buf[i]);	
	}
}

 Cutting method 2:

	char str[] = "aaaa|bbbb|cccc|dddd";
	int i = 0;
	//buf[0] = str
	char* buf[30] = {str};//存放子串首元素地址
	//后续切
	int i = 0;
	while (1)
	{
		buf[i] = strtok(str[i], "|");
		if (buf[i] == NULL)
			break;
		i++;
	}

Simplified cutting method:

	char str[] = "aaaa|bbbb|cccc|dddd";
	//buf[0] = str
	char* buf[30] = {str};//存放子串首元素地址
	//后续切
	int i = 0;
	while ((buf[i] = strtok(buf[i], "|")) && (++i));

 2. String conversion value

1. Function realization

Header file: #include<stdlib.h>

atoi converts numeric strings to int types

atol converts numeric strings to long types

atof converts a numeric string to a float type

2. Code implementation

Guess you like

Origin blog.csdn.net/m0_75045191/article/details/131472312