Added example 2-4. Program compares strings.
[C-Programming-Examples.git] / ex_1-16.c
blobfdf69c54c19945488ad8c102c9397a8870a23dd3
1 #include <stdio.h>
3 #define MAXLINE 1000
5 int getline(char line[], int maxline);
6 void copy(char to[], char from[]);
8 main()
10 int len; /* current line length */
11 int max; /* maximum length seen so far */
12 char line[MAXLINE]; /* current input line */
13 char longest[MAXLINE]; /* longest line saved here */
15 max = 0;
16 while ((len = getline(line, MAXLINE)) > 0)
18 if(len > max)
20 max = len;
21 copy(longest, line);
23 if (max > 0)
24 printf("Longest is %d chars:\n%s", max, longest);
26 printf("\n");
27 return 0;
30 /* getline: read a line into s, return length */
31 int getline(char s[], int lim)
33 int c, i, j;
35 for (i = 0, j = 0; i<lim-1 && (c = getchar()) != EOF && c!='\n'; ++i)
37 if(i < lim - 1)
39 s[j++] = c;
42 if(c == '\n')
44 if(i <- lim - 1)
46 s[j++] = c;
48 ++i;
50 s[j] = '\0';
51 return i;
54 /* copy: copy 'from' into 'to'; assume to is big enough */
55 void copy(char to[], char from[])
57 int i;
58 i = 0;
59 while ((to[i] = from[i]) != '\0')
60 ++i;