Detailed explanation of character functions and string functions

Key points of this chapter

Find the length of the string
strlen
String function with unlimited length
strcpy
strcat
strcmp
String function with limited length Introduction
strncpy
strncat
strncmp
string search
strstr
strtok
error message report
strerror
character operation
memory manipulation function
memcpy
omemmove
memset

strlen

size_t strlen(constchar *str);

1. The string has \0' as the end mark, and the strlen function returns the number of characters that appear before \0 in the string (not including \0').

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

3. Note that the return value of the function is size_t, which is unsigned (error-prone)

4. Learn the simulation implementation of the strlen function.

#include <stdio.h>
int mainO)
{
const charstr1="abcdef";
const char*str2="bbb";
if(strlen(str2)-str1en(str1)>0)
{
printf("str2>str1\n");
else
{
printf("srti>str2\n");
}
return 0;
}

strcpy

char* strcpy(char*destinationconstchar*source);

.Copiesthe Cstring pointed by source into the array pointed by destination,including the terminating nullcharacter (and stopping at that point).

1. The source string must end with \0'.

2. The \0' in the source string will be copied to the target space.

3. The target space must be large enough to ensure that the source string can be stored.

4. The target space must be variable.

5. Learn to simulate the realization.

screwed up

char * strcat (char*destinationconstchar*source):

·Appends a copy of the source string to the destination string.The terminating ull character in

destination is overwritten by the first character of sourceand a null-character is included at the

end ofthe new string formed by the concatenationof both indestination.

1. The source string must end with \0.

1. The destination space must be large enough to accommodate the contents of the source string.

2. The target space must be modifiable.

3. How about adding the string to yourself?

strcmp

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

This function starts comparing the first character ofeach 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 regulation:

oIf the first string is greater than the second string, returns a number greater than 0

o returns 0 if the first string is equal to the second string

oIf the first string is less than the second string, returns a number less than 0

So how to judge two strings?

strncp

char* strncpy (char* destination const charsource,sizet num);

e Copies the first num characters of source to destination.If the end ofthe sourceCstring(which is

signaled by a null-character) is found before num characters have been copieddestination is

padded with zeros until a total of num characters have been written to it.

Copies num characters from source string to destination space.

If the length of the source string is less than num, after copying the source string, add 0 to the end of the target until num

strncat

charstrncat (char destination,const charsourcesize num);

.Appends the first num characters of source to destinationplus a terminating null-character.

e Ifthe length of the Cstring in source is less than um,only the content up to the terminating null

character is copied.

 

/*strncat example"/
#include <stdio.h>
dinclude <string.h>
int main
{
 char str1[20];
 char str2[20];
strcpy(str1,"To be");
strcpy(str2 "or not to be");
strncat(strl,str2,6);
puts(str1);
retorn 0;
}

strncmp

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.

/strncmp examplee/
#include <stdio.h>
#incTude sstring.h>
int main
char str[][5]={"R2D2","C3PO","R2A6"};
inr n;
puts (Looking for R2 astromech droids...");
for (n=0 ;n<3;n++)
if(strncmp(str[n]"R2xx2)-0)
{
printf("found%s\n"str[n]);
}
return 0;}

strstr lookup string

char*strstr(const char*=const char*);

Returns a pointer to the first occurrence of str2 Instr1or a nuill pointer if str2 Is not part of str1..

strtok

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

The sep parameter is a string defining the set of characters used as separators.

The first parameter specifies a string containing 0 or more tags separated by one or more delimiters in the sep string

remember.

The strtok function finds the next token in str, terminates it with \0, and returns a pointer to this token.

(Note: The strtok function will change the string being manipulated, so the strings split using the strtok function are generally temporarily copied

compliant and modifiable. )

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 it in the string

s position.

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.

strerror

chars strerror (int errnum ):

Return the error code and the corresponding error message.

/* strerror example :error list*/

#include <stdio.h>

#include <string.h>

#include<errno.h>//The header file that must be included

function Returns true if its argument meets the following conditions
iscntrl any control characters
isspace Blank characters: space '', form feed\f, line feed\carriage return\r', tab \t or vertical tab \v
even Decimal number 0~9
self digit Hexadecimal numbers, including all + base numbers, 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
the ice hall Letter or number, a~z, A~Z0~9
ispunct Punctuation, any graphic character that is not a number or letter (printable)
isgraph any graphic character
sprint Any printable character, including graphic characters and white space characters

memmove

voidememmove(void*destination,const void*sourcesizet num );

The difference with memcpy is that the source memory block and target memory block processed by the memmove function can overlap.

If the source space and the target space overlap, you have to use the memmove function to deal with it.

memcmp

int memcmp ( const void*ptr1,const void * ptr2,size_t num );

Compare num bytes starting from ptr1 and ptr2 pointers. The return value is as follows:

Guess you like

Origin blog.csdn.net/2301_77479336/article/details/130179412