String handler
gets
#include <stdio.h> char *gets(char *s); Function: read characters from standard input and save them to s The specified memory space until a newline occurs or the end of the file is read. Parameters: s: First address of string Return value: Success: read in string Failed: NULL
The difference between gets and scanf
Differences between gets(str) and scanf('% s', str):
- gets(str) allows the input string to contain spaces
- scanf('% s', str) does not allow spaces
Note: because scanf() and gets() cannot know the size of string s, they must encounter a newline character or receive input until the end of the file. Therefore, it is easy to cause the character array to cross the boundary (buffer overflow).
A method that allows scanf to also receive spaces
char ch[100]; scanf("%[^\n]",ch);
routine
char str[100]; printf("Please enter str: "); gets(str); printf("str = %s\n", str);
puts
#include <stdio.h> int puts(const char *s); Function: standard equipment output s String, which is automatically output after the output is completed'\n'. Parameters: s: First address of string Return value: Success: non negative Failed:-1
Compared with printf, puts will automatically output a '\ n' after the output is completed
routine
#include <stdio.h> int main() { printf("hello world"); puts("hello world"); return 0; }
printf does not add \ n does not print line breaks, and puts will automatically output one after the output is completed \ n
fgets
#include <stdio.h> char *fgets(char *s, int size, FILE *stream); Function: from stream Read in characters from the specified file and save to s The specified memory space until a newline character appears, the end of the file is read, or has been read size - 1 Characters will be added automatically at the end '\0' Ends as a string. Parameters: s: character string size: Specifies the length of the maximum read string( size - 1) stream: File pointer. If you read the string entered by the keyboard, write it as stdin Return value: Success: String successfully read End of file read or error: NULL
When fgets() reads a string entered by the user through the keyboard, it also takes the carriage return entered by the user as a part of the string. When inputting a string through scanf and gets, it does not contain the ending "\ n", but there is more "\ n" at the end through fgets. The fgets() function is safe and there is no buffer overflow problem.
routine
#include <stdio.h> int main() { char str[100]; printf("Please enter str: "); fgets(str, sizeof(str), stdin); printf("str = \"%s\"\n", str); return 0; }
fputs
#include <stdio.h> int fputs(const char * str, FILE * stream); Function: will str Writes the specified string to stream String terminator in the specified file '\0' Do not write to file. Parameters: str: character string stream: File pointer. If the string is output to the screen, it is fixed as stdout Return value: Success: 0 Failed:-1
Fputs() is the file operation version of puts(), but fputs() will not automatically output a '\ n'
routine
#include <stdio.h> int main() { printf("hello world"); puts("hello world"); fputs("hello world", stdout); return 0; }
strlen
#include <string.h> size_t strlen(const char *s); Function: calculate the specified string s The length of the string, excluding the string terminator'\0' Parameters: s: First address of string Return value: String s The length of the, size_t by unsigned int type
be careful:
① Does not contain string terminator '\ 0'
② Need to import library
strcpy
#include <string.h> char *strcpy(char *dest, const char *src); Function: handle src Copy the string pointed to to to dest In the space pointed to,'\0'It will also be copied Parameters: dest: Destination string first address src: Source character prefix address Return value: Success: Return dest First address of string Failed: NULL
Note: if the memory space referred to by the parameter dest is not large enough, it may cause a buffer overflow error.
strncpy
#include <string.h> char *strncpy(char *dest, const char *src, size_t n); Function: handle src Points to the front of the string n Characters copied to dest In the space pointed to, whether to copy the terminator depends on whether the specified length contains'\0'. Parameters: dest: Destination string first address src: Source character prefix address n: Specifies the number of strings to copy Return value: Success: Return dest First address of string Failed: NULL
strcpy and strncpy
#include <stdio.h> #include <stdlib.h> int main() { char a[10] = "hello"; char b[10]; char c[10]; strcpy(b,a); strncpy(c,a,5); printf("%s\n",a); printf("%s\n",b); printf("%s\n",c); return 0; }
strncpy will not automatically copy '\ 0'
strcat
#include <string.h> char *strcat(char *dest, const char *src); Function: will src Connect string to dest The tail of the,'\0'Will also add the past Parameters: dest: Destination string first address src: Source character prefix address Return value: Success: Return dest First address of string Failed: NULL
strncat
#include <string.h> char *strncat(char *dest, const char *src, size_t n); Function: will src Before string n Characters connected to dest The tail of the,'\0'Will also add the past Parameters: dest: Destination string first address src: Source character prefix address n: Specifies the number of strings to append Return value: Success: Return dest First address of string Failed: NULL
Note:
① The appended target string needs enough space
② In append, the \ 0 in the target string is removed first, and then the \ 0 in the source string is appended
③ Limited append note: limited append will also append \ 0
strcmp
#include <string.h> int strcmp(const char *s1, const char *s2); Function: comparison s1 and s2 Character size, yes ASCII Code size. Parameters: s1: First address of string 1 s2: First address of string 2 Return value: Equal: 0 Greater than:>0 Less than:<0
strncmp
#include <string.h> int strncmp(const char *s1, const char *s2, size_t n); Function: comparison s1 and s2 front n The size of characters, compared with characters ASCII Code size. Parameters: s1: First address of string 1 s2: First address of string 2 n: Compares the specified number of strings Return value: Equal: 0 Greater than: > 0 Less than: < 0
sprintf
#include <stdio.h> int sprintf(char *_CRT_SECURE_NO_WARNINGS, const char *format, ...); Function: according to parameters format String to convert and format the data, and then output the result to str In the specified space until the string terminator appears '\0' until. Parameters: str: First address of string format: String format, usage and printf()equally Return value: Success: number of characters actually formatted Failed: - 1
sscanf
#include <stdio.h> int sscanf(const char *str, const char *format, ...); Function: from str Reads the data according to the specified string and reads the data according to the parameters format String to convert and format data. Parameters: str: The first address of the specified string format: String format, usage and scanf()equally Return value: Success: number of parameters, number of successfully converted values Failed: - 1
be careful:
① Like scanf, sscanf needs to add warnings at the beginning of the program
② Return the specific number upon success. Failure - 1
strchr
#include <string.h> char *strchr(const char *s, int c); Function: in string s Find letters in c Location of occurrence Parameters: s: First address of string c: Match letters(character) Return value: Success: returns the first occurrence c address Failed: NULL
strstr
#include <string.h> char *strstr(const char *haystack, const char *needle); Function: in string haystack Find string in needle Location of occurrence Parameters: haystack: First address of source string needle: First address of matching string Return value: Success: returns the first occurrence needle address Failed: NULL
strtok
#include <string.h> char *strtok(char *str, const char *delim); Function: to divide the string into fragments. When strtok()In parameter s Parameter found in string delim When dividing characters contained in, The character is changed to\0 Character. When multiple characters appear in succession, only the first one is replaced with\0. Parameters: str: Point to the string to be split delim: Splits all characters contained in the string Return value: Success: the first address of the split string Failed: NULL
- On the first call: strtok() must give the parameter s string
- In subsequent calls, the parameter s is set to NULL, and each successful call returns a pointer to the segmented fragment
be careful:
① The cutting point is represented by \ 0. After cutting, the original string style will be destroyed
② When multiple split characters appear in succession, only the first one is replaced with \ 0
atoi
#include <stdlib.h> int atoi(const char *nptr); Function: atoi()Can scan nptr String, skip the preceding space character, and do not start the conversion until it encounters a number or sign, but encounter a non number or string terminator('\0')To end the conversion and return the result to the return value. Parameters: nptr: String to be converted Return value: integer after successful conversion
- atof(): converts a decimal string into a floating-point number.
- atol(): converts a string to a long type
Note: ① skip the preceding space character, and do not start the conversion until you encounter a number or sign, while do not end the conversion until you encounter a non number or string Terminator ('\ 0')