Added example 5-14. Sorts data from stdin. Has some issues.
[C-Programming-Examples.git] / ex_1-16.c
blob95ede57fb87f701ee98447fa98bed3c9e79dc731
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);
24 if (max > 0)
25 printf("Longest is %d chars:\n%s", max, longest);
27 printf("\n");
28 return 0;
31 /* getline: read a line into s, return length */
32 int getline(char s[], int lim)
34 int c, i, j;
36 for (i = 0, j = 0; i<lim-1 && (c = getchar()) != EOF && c!='\n'; ++i)
38 if(i < lim - 1)
40 s[j++] = c;
43 if(c == '\n')
45 if(i <- lim - 1)
47 s[j++] = c;
49 ++i;
51 s[j] = '\0';
52 return i;
55 /* copy: copy 'from' into 'to'; assume to is big enough */
56 void copy(char to[], char from[])
58 int i;
59 i = 0;
60 while ((to[i] = from[i]) != '\0')
61 ++i;