当前位置:网站首页>C language string introduction and related operation functions
C language string introduction and related operation functions
2022-07-27 05:37:00 【I'm really not a second dimension】
String and related operation functions
1. character string
1. character
In a computer, characters are stored as integers , When it needs to be displayed as characters , Will be based on ASCII The corresponding relationship in the code table shows the corresponding symbols or patterns
'\0' 0 Null character
'0' 48
'A' 65
'a' 97
2. strand
It's a data structure , It consists of a continuous set of data of the same type , There must be an end sign at the end
For this kind of data structure, it is batch , From the beginning to the end of the processing flag stop
3. character string
A serial structure consisting of characters , The end sign is ’\0’
2. The existence form of string
1. A character array
char str[5]={'a','b','c','d','e'};
from char An array of types , for '\0' Reserved location
Use stack memory , Data can be modified halfway
2. string literal
“ Several characters contained in double quotation marks ”( There is text Cannot modify in segment , String constant )
The end will automatically fill ’\0’
String literals exist as addresses , There is a code segment for character data , If you modify it, it will definitely produce segment errors
Be careful : Exactly the same string literal , There is only one copy in the code segment
const char* p=“ string literal ”;
sizeof§; 4/8
sizeof(“abcd”); 5 Calculate the number of memory bytes occupied by the string literal in the code segment , Include ’\0’
3. Commonly used way
A character array []=“ string literal ”;
Will automatically give ’\0’ Reserved location 、 You can modify the content 、 Initialization is simple
There are two copies of the string after the assignment , One in the code segment 、 A section of memory on the stack ( Can be modified )
3. Input of string 、 Output
1. Input :
char str[256]={};
scanf("%s",str);
shortcoming : You cannot enter a string with spaces
char *gets(char *s);
function : A string with spaces can be entered
shortcoming : Do not limit the input length , There are security risks , The compiler will have a warning
Return value : return s, For chain calls , The return value of one function is directly used as the parameter of another function
char *fgets(char *s,int size,FILE *stream);
function : from stream Maximum input in size-1 Characters to s in
s: A character array
size: The maximum number of characters accepted +1
stream:stdin Equivalent to keyboard file Fixed write
Return value : call chaining
Be careful : If the input exceeds size-1 Characters , Don't accept the extra ; If the input is insufficient size-1 individual ,'\n' Will also receive
2. Output :
printf("%s", A character array / string literal );
int puts(const char *s);
function : Output string
Return value : The number of characters successfully output
Be careful : Will automatically print line breaks
4. String related operation functions
#include <string.h>size_t strlen(const char *s);
function : Calculate the string character length , barring ’\0’
char *strcpy(char *dest,const char *src);
function : hold src Copy to dest, It is equivalent to giving dest assignment =
Return value : call chaining
char *strcat(char *dest,const char *src);
function : hold src Append to dest At the end of , It is equivalent to merging two strings ,+=
Return value : call chaining
int strcmp(const char *s1,const char *s2);
function : Compare the size of two strings
Start from scratch , Compare each character one-on-one , In dictionary order , Who appears in front, who is small , Once the results are compared , Return results immediately , The later ones are no longer compared
Return value :
0 s1==s2
Positive numbers s1>s2
negative s1<s2
int atoi(const char *nptr);
function : String rotation int type long atol(const char *nptr);
function : String rotation long type long long atoll(const char *nptr);
function : String rotation long long type double atof(const char *nptr);
function : String rotation double type
char *strstr(const char *haystack,const char *needle);
function : stay haystack To find out whether there is a substring needle
Return value :needle stay haystack The first place in , Return if not found NULL
int sprintf(char *str,const char *format,...);
function : Output various types of data to string str
Return value : Number of converted characters
int sscanf(const char *str,const char *format);
function : from str Parse and read data into variables
Return value : The number of successfully resolved variables
```void *memcpy(void *dest,const void *src,size_t n);``
function : from src Location copy n Byte to dest The location of
function : call chaining
int memcmp(const void *s1,const void *s2,size_t n);
function : Compare the values of two pieces of memory , Compare by byte , Once the results are compared, there will be no comparison later
Return value :
s1 == s2 0
s1 < s2 negative
s1 > s2 Positive numbers
5. Input buffer
The program does not immediately get the input from the keyboard , But when you press enter , The contents entered by the terminal will be stored in the input buffer first , Then the input function reads data from the input buffer into the content .
1、 When you want to read integer or floating-point data , But when the data in the buffer is symbols or letters , Reading will fail , Data will remain in the buffer , Affect the reading of all data next
solve : Judge scanf Whether all the returned values of are entered correctly , If not , First clear the input buffer and then re-enter , Until all are correct
2、fgets It is acceptable to specify size-1 Characters , If there are extra characters, they will remain in the buffer , It may affect the next input
resolvent 1:
1、 First judge '\n' In or out of the string , If not , It means that it is in the buffer , The buffer needs to be cleaned up
2、 If in the buffer , be
scanf("%*[^\n]");
Read arbitrary data from the buffer and discard , If not '\n', Continue reading , Until I met '\n', Stop reading ( Regular expressions )
scanf("%*c")
Read a character from the buffer , And discard
Be careful : Consider packaging new fgets Function to solve the problem of too many inputs
Solution 2:
Pointer the current position in the input buffer , Move to the end of the buffer , It is equivalent to clearing the input buffer , But only in Linux/Unix These two position pointers are used in the system
stdin->_IO_read_ptr = stdin->_IO_read_end;
3、 Enter an integer first 、 Floating point data , Then type the characters 、 When the string , The previous input will remain '\n', Affect the following characters 、 Input of string
solve :scanf(" %c",&ch);
6. Output buffer
The content to be output and displayed in the program will not be immediately displayed on the screen , Instead, it is stored in the output buffer first , When certain conditions are met, the content will be displayed from the buffer to the screen
1、 encounter ’\n’ after
2、 When encountering an input statement
3、 When the output buffer is full 4k
4、 At the end of the program
5、 Manually refresh fflush(stdout);
边栏推荐
猜你喜欢
随机推荐
块,行内块元素之间存在间隙
Hi3516DV300环境搭建
Trying to evolve_ My first CSDN blog
Xiaomi mall project_ register
Flask请求数据获取与响应
封装JWT
用户登录-以及创建、验证短信验证码
md5 密码加密
初识C语言——常量、变量
李宏毅机器学习组队学习打卡活动day05---网络设计的技巧
数据库迁移报错解决
C语言做一个小迷宫
Day5 --- Flask-RESTful请求响应与SQLAlchemy基础
pytorch安装新坑
Multiplication sorting in torch, * & torch. Mul () & torch. MV () & torch. Mm () & torch. Dot () & @ & torch. Mutmal ()
JS基础知识--每日学习总结①
C语言进制转换以及原补反码位运算介绍
arguments
Common commands in CONDA and pip environments
Flask登录实现









