当前位置:网站首页>Strings in C language
Strings in C language
2022-07-23 08:09:00 【Programmer_ Xuyih】
C Strings in languages
One 、 character string
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
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
The processing of this data structure is batch , From the beginning to the end of the processing flag stop
character string :
A serial structure consisting of characters , The end sign is ‘\0’
Two 、 The existence form of string
A character array :
char str[5] = {'a','b','c','d','e'};
from char An array of types , for '\0' Reserved location
Stack memory is used , Data can be modified halfway
string literal :
" Several characters contained in double quotation marks "
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(p) = 4/8
sizeof("abce") = 5 Calculate the number of memory bytes occupied by the string literal in the code segment , Include '\0'
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 、 One in stack memory ( Can be modified )
3、 ... and 、 Input of string 、 Output
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
Return value : return s The first address , For chain calls , The return value of one function is directly used as the parameter of another function
shortcoming : Do not limit the input length , There are security risks , The compiler will have a warning
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 more
If the input is insufficient size-1, '\n' Will also receive
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
Four 、 Output buffer
The content to be output 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 displayed from the buffer will be displayed on 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);
5、 ... and 、 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 Is the return value of all data received input correct , If not , First clear the input buffer and then re-enter , Until all are correct
2、fgets Can receive specified 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
resolvent 2:
Look at the current position pointer of 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 positions 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、 ... and 、 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
String correlation function :
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
Return value : 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
边栏推荐
- php可不可以拆分数组
- 大咖访谈 | 开源社区里各种奇怪的现状——夜天之书陈梓立tison
- 1.11 ArrayList & student management system
- Text align: center centered
- General, special and hidden attributes of the file (instance generates animation)
- Rust -- detailed explanation of option
- 二叉树(学习日常)
- php数组下标是不是只能从0开始
- 怎么使用selenium.chrome实现扩展拦截或转发请求功能
- Internet traffic scheduling scheme
猜你喜欢

使用Hystrix实现容错处理

Worthington哺乳动物乳酸脱氢酶研究——特点及测定方案

How to use C language to realize simple employee information management system

21 -- product of arrays other than itself

matlab声音信号处理 频率图 信号过滤和播放声音

Experiment 4 DPCM

昇思易点通 | 经典卷积神经网络的深度学习解析

Can the formatted data of the USB flash disk be recovered? How to recover the formatted data of the USB flash disk

Worthington纯化酶制剂助力新生儿心肌细胞分离系统研究

PIP update a package
随机推荐
来,滑动到下一个小姐姐
技术干货 | 基于MindSpore详解Perplexity语言模型评价指标
General, special and hidden attributes of the file (instance generates animation)
高精度移相(MCP41xx)程序stm32F103,F407通用,更改引脚即可(SPI软件模拟通信)
树以及二叉树的常用性质以及遍历
PIP update a package
Can PHP array subscripts only start from 0
C language minesweeping
How to use selenium.chrome to realize the extended function of intercepting or forwarding requests
c语言十进制数转二进制数
H7-TOOL串口脱机烧录操作说明,支持TTL串口,RS232和RS485(2022-06-30)
MySQL消息队列表结构
Experiment 7 H.264 file analysis
敏捷测试团队组织构成
Celebrity interview | various strange current situations in the open source community -- night sky Book Chen Zili tison
C语言函数(1)
轻松带你走进turtle绘图的大门
Hcip --- BGP comprehensive experiment
Change this point to understand
Experiment III LZW