Added partial tutorial 5-5. Function copies n chars from source to destination.
[C-Programming-Examples.git] / char_arrays.c
blob7777e981fb8cb955946d8eb299a4a7ba6b406f6a
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)
17 if(len > max)
19 max = len;
20 copy(longest, line);
22 if (max > 0)
23 printf("%s", longest);
24 return 0;
27 /* getline: read a line into s, return length */
28 int getline(char s[], int lim)
30 int c, i;
32 for (i = 0; i<lim-1 && (c = getchar()) != EOF && c!='\n'; ++i)
33 s[i] = c;
34 if(c == '\n')
36 s[i] = c;
37 ++i;
39 s[i] = '\0';
40 return i;
43 /* copy: copy 'from' into 'to'; assume to is big enough */
44 void copy(char to[], char from[])
46 int i;
47 i = 0;
48 while ((to[i] = from[i]) != '\0')
49 ++i;