Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Application of C language string
Application of C language string
trcpy(p, p1) copy the string

strncpy(p, p1, n) copy the string of specified length

strcat(p, p1) append the string of specified length

strlen(p) take the string length < P1) compare strings

strcassecmp ignore case comparison strings

strncmp(p, p1, n) compare strings of specified length

strchr(p, c) find specified characters in strings

strchr (p, c) find

strstr(p, P1) looking for the character string

strbprk (p, P1) takes all the characters of the target string as a set, looking for any element of the set in the current string

strspn(p, p1) takes all the characters of the target string as a set, and looking for the offset of any element that does not belong to the set in the current string

strcspn(p, P1) take all the characters of the target string as a set, and find the offset of any element belonging to the set in the current string

* The string processing function with a specified length fills the zero-ending character after the processed string

2) The conversion from string to numeric type

Strod (p, ppend) converts the numeric value of double type from string p, and stores the subsequent string pointer to the char* type pointed by ppend.

strtol(p, ppend, base) converts an integer value of type long from the string p, and base explicitly sets the converted integer system, which is set to to judge the used system according to a specific format, and x, X prefix is interpreted as an integer in hexadecimal format. prefix to be interpreted as octal format integer

atoi(p) string converted to int integer

atof(p) string converted to double symbol point

atol(p) string converted to long integer

3) character check

isalpha () check whether it is an alphabetic character

isupper (). Check whether it is an uppercase alphabetic character

islower () Check whether it is a lowercase alphabetic character

isdigit () Check whether it is a number

isxdigit () Check whether it is a valid character represented by hexadecimal digits

isspace () Check whether it is a space type character

iscntrl (). Check whether it is a control character

ispfunction () Check whether it is a punctuation mark

isalnum () Check whether it is a letter and a number

isprint () Check whether it is a printable character

isgraph () Check whether it is a graphic character, which is equivalent to isalnum () | ispfunction ()

4) Function prototype

.

function: copy the string source into the string destination

routine:

# include

# include

void main (void)

{

charstr 1 [1] = {"tsinghua ok"};

  char str2[1] = { "Computer"};

  cout < < strcpy(str1,str2)< < endl;

}

The running result is: Computer

The second string will overwrite all the contents of the first string!

note: when defining an array, the string length of character array 1 must be greater than or equal to the string length of string 2. You cannot assign a string constant or a character array directly to a character array with an assignment statement. All string handling functions are contained in the header file string.h

strncpy(char destination[], const char source[], int numchars);

strncpy: copy the first numchars characters in the string source into the string destination.

application example of strncpy function

prototype: strncpy (chardestination [], constcharsource [], int numchars);

function: copy the first numchars characters in the string source into the string destination

routine:

# include

# include

void main (void)

{

charstr 1 [1] = {"tsinghua"};

  char str2[1] = { "Computer"};

  cout < < strncpy(str1,str2,3)< < endl;

}

Running result: Comnghua

Note: the first numchars characters in the string source will overwrite the first numchars characters in the string destination!

prototype: strcat (chartarget [], constcharsource []);

function: connect the string source after the string target

routine:

# include

# include

void main (void)

{

charstr1 [] = {"tsinghua"};

  char str2[] = { "Computer"};

  cout < < strcpy(str1,str2)< < endl;

}

Running result: Tsinghua Computer

Note: the length of character array 2 should be considered when defining the length of character array 1, because the length of the new string after connection is the sum of the lengths of two strings. After string concatenation, the ending character of string 1 will be automatically removed, and a ending character after the new string will be kept at the end of the ending string.

prototype: strncat (chartarget [], constcharsource [], int numchars);

function: connect the first numchars characters of the string source to the back of the string target

routine:

# include

# include

void main (void)

{

charstr 1 [] = {"tsinghua"};

  char str2[] = { "Computer"};

  cout < < strncat(str1,str2,3)< < endl;

}

running result: Tsinghua Com

prototype: int strcmp (constchar firststring [], constchar second string);

function: compare two strings firststring and secondstring

routine:

# include

# include

void main (void)

{

charbuf1 [] = "AAA";

  char buf2[] = "bbb";

  char buf3[] = "ccc";

  int ptr;

  ptr = strcmp(buf2,buf1);

  if(ptr > )

cout < < "Buffer 2 is greater than buffer 1"< < endl;

  else

cout < < "Buffer 2 is less than buffer 1"< < endl;

  ptr = strcmp(buf2,buf3);

  if(ptr > )

cout < < "Buffer 2 is greater than buffer 3"< < endl;

  else

cout < < "Buffer 2 is less than buffer 3"< < endl;

}

the running result is: buffer2isless than buffer1

buffer2isgreater than buffer3

prototype: strlen( const char string[]);

function: count the number of characters in the string

routine:

# include

void main (void)

{

charstr [1];

cout < < "Please enter a string:";

cin > > str;

cout < < "The length of the string is :"< < strlen(str)< < "Ge" < < endl;

}

Run the result The length of the string is x (x is the total number of characters you entered)

Note: the function of strlen function is to calculate the actual length of the string, excluding' \'. In addition, the strlen function can also directly test the length of string constants, such as strlen("Welcome ").

void *memset(void *dest, int c, size_t count);

set the count characters before dest as the character C. return the value of dest.

void * memmove (void * dest, constvoid * src, size _ tcount);

copy the characters of count bytes from src to dest. If src and dest overlap, the function will automatically handle it. Return the value of dest.

Void * memcpy (void * dest, constvoid * src, size _ tcount);

copy the characters of count bytes from src to dest. It is the same as the memmove function, except that it cannot handle the overlap between src and dest. Return the value of dest.

Void * memchr (constvoid * buf, int c, size _ tcount);

find the position of the first occurrence of the character c in the count bytes before buf. If the character c is found or the count bytes have been searched, the search will stop. If the operation is successful, the position pointer of the first occurrence of c in buf will be returned, otherwise null will be returned.

Void * _ memccpy (void * dest, constvoid * src, int c, size _ t count);

copy or more bytes of characters from src to dest. when character c is copied or count characters are copied, the copying stops.

if character c is copied, the function returns a pointer next to the character position. otherwise, it returns null.

int memcmp (constvoid * buf1, constvoid * buf2, size _ tcount);

compare the size of count bytes before buf1 and buf2.

return value <; , indicating that buf1 is less than buf2;

the return value is , which means that buf1 is equal to buf2;

return value > , indicating that buf1 is greater than buf2.

int mem icmp (constvoid * buf1, constvoid * buf2, size _ tcount);

compare the number of bytes before buf1 and buf2. Unlike memcmp, it is case-insensitive.

The return value is the same as above.

char *strrev(char *string);

reverse the order of the characters in the string. The NULL terminator position remains unchanged. Return the pointer to the adjusted string.

char *_strupr(char *string);

write all lowercase letters in the string.