How to express the string in C language
This article mainly introduces the relevant knowledge of how to express the C language string, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this C language string. Let's take a look.
String representation
String constant
Content enclosed in double quotation marks is called a string constant, for example: "Hello, World!" Is a string constant. The characters in double quotes and the\ 0 characters at the end of the compiler are stored in memory as strings.
String constants belong to the static storage category. When a string constant is used in a function, the string is stored only once, and the content enclosed in double quotes is treated as a pointer to the storage location of the string, as shown in the following routine:
/ * strptr.c-think of a string as a pointer * / # include int main (void) {printf ("% s,% p,% c\ n", "Who", "you", * "are"); return 0;}
The output of the program is as follows:
Who, 0x400668, a
# strlen
Before using the strlen function, you need to add a header file: # include
Function declaration: size_tstrlen (const char * s)
Function function: gets the length of the string, excluding'\ 0'.
Return value: returns the number of characters in the string (spaces are also a character)
Example:
# inclue#includeint main () {char str [] = "hello world"; int len=strlen (str); printf ("% d", len); / / len=11} # strcpy and strncpy##strcpy
Function declaration: char * strcpy (char * dest,const char * src)
Function description: copy the string pointed by src to the memory pointed to by the dest pointer, and'\ 0' will also be copied.
Function return value: the address of the destination memory.
Note: when using this function, you must make sure that the memory space pointed to by dest is large enough, otherwise memory contamination will occur.
Example:
# include#include int main () {char str [] = "hello world"; char s [] = "hello earth"; strcpy (str,s); printf ("% s", str); / / the printed result is hello earth} # # strcnpy
Function declaration: char * strncpy (char * dest,const char * src,size_tn)
Function description: copy the first n bytes of the string pointed to by src to the memory pointed to by dest.
Return value: the first address of the destination memory.
Note: 1.strncpy does not copy'\ 0'.
two。 If n is greater than the number of characters in the string pointed to by src, fill in n-strlen (src)'\ 0' after dest.
Example:
# include#include int main () {char str [] = "hello world"; char s [] = "hello earth"; strncpy (str,s,8); printf ("% s", str);}
# strcat and strncat##strcat
Function declaration: char * strcat (char * dest,const char * src)
Function function: the strcat function appends the src string to the string pointed to by dest, and appends'\ 0' when it is appended.
Note: make sure that the memory space pointed to by dest is large enough.
Example:
# include#include int main () {char str [100] = "hello world"; char s [] = "hello"; strcat (str,s); printf ("% s", str);}
# # strncat
Function declaration: char * strncat (char * dest,const char * src,size_tn)
Append the first n characters of the string pointed to by src to the end of the string pointed to by dest.
Note: if n is greater than the number of characters in src, only the src string will be appended to the string pointed to by dest, and'\ 0' will be appended when appended.
Example:
# include#include int main () {char str [100] = "hello world"; char s [] = "hello"; strncat (str,s,4); printf ("% s", str);}
# strcmp and strncmp##strcmp
Function declaration: int strcmp (const char * S1 Magi Const char * S2)
Function description: compare the size of the string pointed to by S1 and S2.
The method of comparison: compare the ASCII code character by character, and return once the size is compared.
Return value:
If S1 points to a string greater than S2 points to, return 1
If the string pointed to by S1 is less than the string pointed to by S2, return-1
Returns 0 if equal
Example:
# include#include int main () {char str1 [] = "hello world"; char str2 [] = "hello world"; int ret; ret=strcmp (str1,str2); printf ("% d", ret); / / ret==0} # # strncmp
Function declaration: int strncmp (const char * S1 Magneto Const char * S2 Magi sizetellt n)
Function description: compare the first n characters in the string pointed to by S1 and S2
Example:
# include#include int main () {char S1 [] = "hello world"; char S2 [] = "hello earth"; int ret; ret=strncmp (S1 printf (ret > 0) printf ("the first five characters of S1 is greater than the first five characters of S2\ n"); else if (ret)