one , Reading and writing of string

1.printf And puts

printf Very familiar , There is also a function that outputs a string puts(), Its usage is as follows . It is worth noting that it will wrap automatically after output .
puts(str);
2.scanf And gets

use scanf The read string is as follows :( Note that address characters are not required & Yes , because str Is the array name , When the compiler passes it to a function, it treats it as a pointer .)
scanf("%s",str);
use scanf Null characters cannot be read in when reading a string , Line breaks can also cause scanf Stop reading in ( Spaces and tabs have the same effect )

In order to read in a whole line at a time, the input can be used gets function .gets The function does not skip the reading of whitespace characters , It doesn't stop until a newline is found .

Because the length cannot be specified , Therefore, these are not safe to read into the array ,scanf have access to %ns To indicate the maximum number of characters entered , and gets Inherently unsafe ( have access to fgets function )

3.fgets function

# include <stdio.h>
char *fgets(char *s, int size, FILE *stream);

Function prototype , Returns an array of characters , There are three parameters , The first one represents the first address stored in the array , The second represents the length of the read string , The third indicates which input stream to read , Such as standard input stream stdin( There is not much explanation here )

two , Accessing characters in a string

1. Array subscript access

2. Pointer self increment and self decrement access

three , use C String library for languages

# include <string.h> In the head

1.strcpy function ( String copy )

prototype :

char *strcpy(char *s1,const char *s2);

Put string s2 Copy to s1( hold s2 Copy characters from to point s1 In the array of ), But it's dangerous ,strcpy Function cannot be checked str2 Is the size of the string pointed to really appropriate str1 Array pointed to .

There is another function strncpy() A third parameter is used to limit the number of characters copied, which can be used in this way
strncpy(str1,str2,sizeof(str1) - 1); str1[sizeof(str1) - 1] = '\0';
2.strlen function ( Find string length )

prototype :

size_t strlen (const char *s);

size_t Type is an unsigned integer type , Unless it's dealing with extremely long strings , We can treat it as an integer . That is, the return type is understood as an integer .

strlen Function returns a string s Length of :s The number of characters before the first null character in the ( Excluding empty characters )

3.strcat function ( String splicing )

prototype :

char *strcat(char *s1,const char *2);

strcat Function string s2 Append the contents of to the string s1 End of , And returns a string s1( Pointer to string ), Of course, if str1 The length of the is not enough to accommodate str2 The character in the string pointed to , It is also dangerous .

There are also strncat function , Has a third parameter to limit the number of characters copied .
strncat(str1,str2,sizeof(str1) - strlen(str1) -1);
calculation str1 Space remaining in , Then subtract 1 Leave space for empty characters .

4.strcmp function

strcmp( string comparison )

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

s1 less than s2  Return less than 0 Value of

s1 be equal to s2 return 0

s1 greater than s2 Return greater than 0 Value of

In fact, it can be understood as doing badly ,s1-s2

 

Technology