I want to Analyze the best and worst cases of the number of calls to the copy function. What will be the difficulty for the average case? please help me to understand this solution?
I think best case is 1 and worst n-1 am I right?
#include <stdio.h>
#define MAXLINE 1000 /* maximum input line size */
int get_line(char line[], int maxline);
void copy(char to[], char from[]);
/* print longest input line */
int main()
{
int es;
int len; /* current line length */
int max; /* maximum length seen so far */
char line[MAXLINE]; /* current input line */
char longest[MAXLINE]; /* longest line saved here */
es=0;
max = 0;
while ((len = get_line(line, MAXLINE)) > 0)
if (len > max)
{
es++;
max = len;
copy(longest, line);
}
printf("%d",es);
if (max> 0) /* there was a line */
printf("\nlongest is:%s\n", longest);
return 0;
}
/* get_line: read a line into s, return length */
int get_line(char s[], int lim)
{
int c, i;
for (i=0; i<lim-1 && (c=getchar()) !=EOF && c!='\n'; ++i)
s[i] = c;
if (c == '\n')
{
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
/* copy: copy 'from' into 'to'; assume to is big enough */
void copy(char to[], char from[])
{
int i = 0;
while ((to[i] = from[i]) != '\0')
++i;
}