[C language] String function

✨Author: @ordinary person 1

✨Column: "C language from 0 to 1"

✨One sentence: everything in the past is a prologue

✨Description: The past is irreversible, the future can be changed


We have learned 4 functions earlier - strlen\strcpy\strcat\strcmp. The length of these functions is unlimited. Today, we naturally want to introduce some other functions. The content may be relatively large.

Length-limited string functions

strncpy

image-20220707091932481

char * strncpy ( char * destination, const char * source, size_t num );
  1. 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.
  2. Copies num characters from the source string to the destination space.
  3. If the length of the source string is less than num, after copying the source string, append 0 to the end of the target until num.

Now, let's do a simple test:

#include <stdio.h>
#include <string.h>
int main()
{
    
    
	char arr1[20] = "abcdef";
	char arr2[] = "hello world";
	strncpy(arr1, arr2, 5);
	printf("%s\n", arr1);
	return 0;
}

run:image-20220707093803269

Let's look at the abovepoint 3:

#include <stdio.h>
#include <string.h>
int main()
{
    
    
	char arr1[20] = "abcdef";
	char arr2[] = "ghi";
	strncpy(arr1, arr2, 5);//arr2只有3个这里却要拷贝5个,这是怎么一回事呢
	printf("%s\n", arr1);
	return 0;
}

F10 debugging to see how arr1 and arr2 look like before copying:image-20220707094657476

After the copy is complete?image-20220707094744118

We can clearly see that when the content is not enough, it will be supplemented with '\0'!

strncat

image-20220707095134661

char * strncat ( char * destination, const char * source, size_t num );
  1. Appends the first num characters of source to destination, plus a terminating null-character.
  2. If the length of the C string in source is less than num, only the content up to the terminating null-character is copied.
#include <stdio.h>
#include <string.h>
int main()
{
    
    
	char arr1[20] = "hello\0xxxxx";
	printf("%s\n", arr1);
	char arr2[] = "world";
	strncat(arr1, arr2, 3);
	printf("%s\n", arr1);
	return 0;
}

We all know the result, so what does the process look like? Let's debug and take a look: before appending:

After appending:image-20220707095641466

We can clearly see that a '\0' will be automatically added at the end

What if the length of the append is greater than itself? Will it make up as many '\0' as strncpy does? Test a piece of code:

#include <stdio.h>
#include <string.h>
int main()
{
    
    
	char arr1[20] = "hello\0xxxxx";
	printf("%s\n", arr1);
	char arr2[] = "abc";
	strncat(arr1, arr2, 6);
	printf("%s\n", arr1);
	return 0;
}

Before appending:image-20220707100015102

After appending:image-20220707100040572

The answer is no, just add a '\0'. Through simple analysis, we also probably know the principle of strncat.

strncmp

image-20220707100258557

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

Compare until another character is different or a string ends or all num characters are compared.

#include <stdio.h>
#include <string.h>
int main()
{
    
    
	char arr1[] = "abcdef";
	char arr2[] = "abc";
	int ret = strncmp(arr1, arr2, 4);
	printf("%d\n", ret);
	if (ret == 0)
	{
    
    
		printf("==\n");
	}
	else if (ret < 0)
	{
    
    
		printf("<\n");
	}
	else
	{
    
    
		printf(">\n");
	}
	return 0;
}

image-20220707101140742

In fact, if these functions have more n, there will be more length restrictions, and there is not much difference. The length-restricted string functions make the code more rigorous, and we try to use them as much as possible.

string lookup

strstr

image-20220707101413372

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.

Simple understanding, this function is a function of finding substrings

#include <stdio.h>
#include <string.h>
int main()
{
    
    
	char email[] = "[email protected]";
	char substr[] = "eichang";
	char*ret = strstr(email, substr);
	if (ret == NULL)
	{
    
    
		printf("子串不存在\n");
	}
	else
	{
    
    
		printf("%s\n", ret);
	}
	return 0;
}

image-20220707101942440

It's not the point about how to use it, the point is how to simulate it!

Mock implementation of strstr

Let's talk about the search process first:

It can be divided into two cases to illustrate:

One is the simple case: one match finds

The other is a more complicated situation: the first match is not found, the current position needs to be recorded, and the matching continues, it needs to be searched many times to find it

image-20220707103859541

The following is a simple simulation implementation:

#include <assert.h>
#include <stdio.h>
char*my_strstr(const char*str1,const char*str2)
{
    
    
	assert(str1 && str2);
	const char* s1 = str1;
	const char* s2 = str2;
	const char* p = str1;
	while (*p)
	{
    
    
		s1 = p;
		s2 = str2;
		while (*s1!='\0'&&*s2!='\0'&&* s1 == *s2)
		{
    
    
			s1++;
			s2++;
		}
		if (*s2 == '\0')
		{
    
    
			return (char*)p;
		}
		p++;
	}
	return NULL;
}

int main()
{
    
    
	char email[] = "[email protected]";
	char substr[] = "eichang";
	//char*ret = strstr(email, substr);
	char* ret = my_strstr(email, substr);
	if (ret == NULL)
	{
    
    
		printf("子串不存在\n");
	}
	else
	{
    
    
		printf("%s\n", ret);
	}
	return 0;
}

Finding substrings can be implemented using the KMP algorithm, but it is more complicated and will not be explained here.

strtok

image-20220707105048477

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

The sep parameter is a string that defines the set of characters to be 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 token in str, ends it with \0, and returns a pointer to this token. (Note: The strtok function will change the string being manipulated, so the string segmented using the strtok function is generally the content of a temporary copy 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 look for the next token.

Returns a NULL pointer if no more tokens exist in the string.

I think this is a strange function, but it does not prevent us from knowing that it cuts strings

#include <stdio.h>
#include <string.h>
int main()
{
    
    
	const char* sep = "@.";
	char email[] = "[email protected]";
	char cp[30] = {
    
     0 };
	strcpy(cp, email);

	char*ret = strtok(cp, sep);
	printf("%s\n", ret);
	ret = strtok(NULL, sep);
	printf("%s\n", ret);
	ret = strtok(NULL, sep);
	printf("%s\n", ret);
	return 0;
}

image-20220707105512359

How to connect with for loop❓

#include <stdio.h>
#include <string.h>
int main()
{
    
    
	const char* sep = "@.";
	char email[] = "[email protected]";
	char cp[30] = {
    
     0 };
	strcpy(cp, email);

	char* ret = NULL;
	for (ret = strtok(cp, sep); ret != NULL; ret = strtok(NULL, sep))
	{
    
    
		printf("%s\n", ret);
	}
	return 0;
}

error message report

strerror

image-20220707105848512

char * strerror ( int errnum );

Returns the error code and the corresponding error message.

#include <stdio.h>
#include <string.h>
int main()
{
    
    
	printf("%s\n", strerror(0));
	printf("%s\n", strerror(1));
	printf("%s\n", strerror(2));
	printf("%s\n", strerror(3));
	printf("%s\n", strerror(4));
	printf("%s\n", strerror(5));
}

image-20220707110144327

These do not need us to remember that a global error code set by the error-C language is stored in the variable

for example:

#include <stdio.h>
#include <string.h>
int main()
{
    
    
    FILE* pf = fopen("test.txt", "r");
	if (pf == NULL)
	{
    
    
		printf("%s\n", strerror(errno));
		return 1;
	}
	else
	{
    
    

	}

    return 0;
}

image-20220707110347132

character classification function

These functions are quite numerous and scattered, and I will not give examples one by one. Here, you can get to know them and practice them yourself:

The function returns true if its arguments meet the following conditions

iscntrl any control character

isspace whitespace characters: space ' ', form feed '\f', line feed '\n', carriage return '\r', tab '\t' or vertical tab '\v'

isdigit Decimal digits 0~9 isxdigit Hexadecimal digits, including all decimal digits, lowercase letters a f, uppercase letters A F

islower lowercase letters a~z

isupper capital letters A~Z

isalpha letter a z or A Z

isalnum letters or numbers, a z, A Z, 0~9

ispunct punctuation mark, any graphic character that is not a number or letter (printable)

isgraph any graphic character

isprint any typeable character, including graphic characters and whitespace

#include <stdio.h>
#include <ctype.h>
int main()
{
    
    
    int i = 0;
    char str[] = "Test String.\n";
    char c;
    while (str[i])
    {
    
    
        c = str[i];
        if (isupper(c))
            c = tolower(c);
        putchar(c);
        i++;
    }
    return 0;
}

image-20220707111234962


Guess you like

Origin blog.csdn.net/weixin_60478154/article/details/125655565