当前位置:网站首页>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
边栏推荐
- Decorator
- Es snapshot based data backup and restore
- sed编辑器
- Reprint - easy to use wechat applet UI component library
- MySQL数据库讲解(四)
- Bifu divides EtherCAT module into multiple synchronization units for operation -- use of sync units
- Hdu1724[Simpson formula for integral]ellipse
- mysql配置提高数据插入效率
- 防火墙介绍
- [shell] generate strings between specified dates
猜你喜欢

ES基於Snapshot(快照)的數據備份和還原

Decorator

Guruiwat rushed to the Hong Kong stock exchange for listing: set "multiple firsts" and obtained an investment of 900million yuan from IDG capital

12 SQL optimization schemes summarized by old drivers (very practical)

shell脚本详细介绍(四)

Here document interaction free and expect automatic interaction

Select tag - uses the default text as a placeholder prompt but is not considered a valid value

Ring queue PHP
![[how to connect the network] Chapter 2 (middle): sending a network packet](/img/5f/602c59d3a7ee1154c7410fb2fcfc41.png)
[how to connect the network] Chapter 2 (middle): sending a network packet

Learn how to develop owl components by hand (7): practical use of owl projects
随机推荐
POJ 3070 Fibonacci
7-3 minimum toll
输入文本自动生成图像,太好玩了!
7-1 n queen problem
Mysql database explanation (IV)
Design of simple digital circuit traffic light
sed编辑器
Detailed sorting of HW blue team traceability process
7-1 range of numbers
Jenkins build prompt error: eacces: permission denied
[proteus simulation] Arduino uno key start / stop + PWM speed control DC motor speed
MongoDB系列之适用场景和不适用场景
Beifu PLC obtains system time, local time, current time zone and system time zone conversion through program
Electron official docs series: References
Here document interaction free and expect automatic interaction
Electron official docs series: Development
KITTI Tracking dataset whose format is letf_ top_ right_ bottom to JDE normalied xc_ yc_ w_ h
Zoomeeper sets ACL permission control (only specific IP access is allowed to enhance security)
[shell] generate strings between specified dates
Wechat applet -picker component is repackaged and the disabled attribute is added -- below