当前位置:网站首页>2021-10-18 character array
2021-10-18 character array
2022-06-26 13:41:00 【Tom looks at programming】
1.
Read a set of text lines from the terminal , And print the longest line of text
Code :
#define MAXLINE 1000
int getline(char line[],int maxline);
void copy(char to[],char from[]);
main()
{
int len;
int max;
char line[MAXLINE];
char longest[MAXLINE];
max=0;
while((len = getline(line,MAXLINE))>0)
if (len > max){
max = len;
copy(longest,line);
}
if (max = 0)
printf("%s",longest);
}
int getline(char s[],int lim)
{
int c,i;
for (i=0;i<lim-1&&c!='\n'&(c=getchar())!=EOF;i++)
s[i] = c;
if(c=='\n'){
s[i] = '\n';
i++;
}
s[i] = '\0';
return i;
}
void copy(char to[],char from[])
{
int i;
i = 0;
while((to[i] = from[i]) != '\0')
++i;
}
analysis :
1, Two data structures are required :line,longest Save the current input line , The longest line ,line longest All points to a size of MAXLINE Of char Array .
2, getline function : Read a line into line in , Return its length .
(a):for The judgment condition of cycle ,i < lim-1 && (c = getchar()) != EOF && c != '/n', When these conditions are met , Read in... From the terminal
Put characters in s Array ( Argument line Array ) in .
(b):Q: Why not i <= lim-1 Well ? A: The end of the string must be '/0', If set to i <= lim-1, In this case i <= 999,
If a string happens to be 1000 Characters , When reading the first 1000 In characters , And the character is not '/n', It didn't reach the end of the file , It's in line with for
The judgment condition of cycle , perform s[i] = c;, At this point, the character array s( Corresponding arguments line) It's full of , When jumping out of the loop, execute s[i] = '/0'; when , because
Space is not enough , An out of bounds error has occurred .
(c): When read in '/n' when , It means that the operation of reading a line of characters from the input is about to be completed , What needs to be done at this time is to '/n' and '/0' Put in s[i] in .
(d): When the read character is EOF, Means the end of the file is reached , There are no more characters to read in the input , So Zaijiang /0' Read in to s in , To form a string ,
This string is stored in a character array s in .
————————————————
Copyright notice : This paper is about CSDN Blogger 「roma823」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/roma823/article/details/6334517
边栏推荐
- mysql配置提高数据插入效率
- Go language - pipeline channel
- 微信小程序注册指引
- Mysql database explanation (6)
- Analysis of state transition diagram of Beifu NC axis
- What is the use of index aliases in ES
- Electron official docs series: Best Practices
- LAMP编译安装
- Lamp compilation and installation
- Beifu realizes the control of time slice size and quantity through CTU and ton
猜你喜欢

Analysis of state transition diagram of Beifu NC axis

Detailed sorting of HW blue team traceability process

Oplg: new generation cloud native observable best practices

Mediapipe gestures (hands)

Embedded virlog code running process

Es6: iterator

三维向量的夹角

Beifu PLC model selection -- how to see whether the motor is a multi turn absolute value encoder or a single turn absolute value encoder

使用 Performance 看看浏览器在做什么

Electron official docs series: Processes in Electron
随机推荐
GO语言-管道channel
Generate JDE dot train
7-2 a Fu the thief
Connection migration for DataGrid configuration
MySQL数据库讲解(三)
Beifu PLC realizes zero point power-off hold of absolute value encoder -- use of bias
Here Document免交互及Expect自动化交互
【MySQL从入门到精通】【高级篇】(二)MySQL目录结构与表在文件系统中的表示
MySQL数据库讲解(六)
Bigint: handles large numbers (integers of any length)
Lamp compilation and installation
【Proteus仿真】Arduino UNO按键启停 + PWM 调速控制直流电机转速
CloudCompare——泊松重建
嵌入式virlog代码运行流程
[shell] generate strings between specified dates
Beifu realizes the control of time slice size and quantity through CTU and ton
MySQL explanation (I)
Echart stack histogram: add white spacing effect setting between color blocks
Detailed practical sharing, two hours of funny videos after work, earning more than 7000 a month
Wechat applet SetData dynamic variable value sorting