当前位置:网站首页>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
边栏推荐
- Teacher Li Hang's new book "machine learning methods" is on the market! Purchase link attached
- I met the problem of concurrent programming in an interview: how to safely interrupt a running thread
- VTK 圆柱体的生成与渲染
- Wechat applet - bind and prevent event bubble catch
- Variable declaration of typescript
- Firewall introduction
- CloudCompare——泊松重建
- Mongodb series window environment deployment configuration
- Basic type of typescript
- [how to connect the network] Chapter 2 (middle): sending a network packet
猜你喜欢

Electron official docs series: Processes in Electron

Common faults of MySQL database - forgetting database password

HW蓝队溯源流程详细整理

C language: Exercise 2

CloudCompare——泊松重建

Ring queue PHP

去某东面试遇到并发编程问题:如何安全地中断一个正在运行的线程

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

Awk tools

Beifu realizes the control of time slice size and quantity through CTU and ton
随机推荐
tauri vs electron
KITTI Tracking dataset whose format is letf_ top_ right_ bottom to JDE normalied xc_ yc_ w_ h
Applicable and inapplicable scenarios of mongodb series
Update and download of Beifu EtherCAT XML description file
泰山OFFICE技术讲座:使用字体粗体的四种情形
C language: Exercise 2
MySQL讲解(二)
7-1 n queen problem
遍历指定目录获取当前目录下指定后缀(如txt和ini)的文件名
Es common grammar I
Composite mode
Mysql database explanation (6)
HDU 3709 Balanced Number
Detailed practical sharing, two hours of funny videos after work, earning more than 7000 a month
CloudCompare——泊松重建
NVM installation tutorial
KITTI Detection dataset whose format is letf_ top_ right_ bottom to JDE normalied xc_ yc_ w_ h
Electron official docs series: References
MySQL数据库讲解(四)
mysql配置提高数据插入效率