Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
oorrja
GitHub Repository: oorrja/learntosolveit
Path: blob/master/languages/cprogs/Ex_1.16_LongLine.c
1240 views
1
/**
2
* Exercise 1.16
3
*
4
* Write a program to print the length of an arbitrarily long input line
5
* and as much text as possible
6
*
7
**/
8
9
10
#include <stdio.h>
11
12
/* The size of the output that we would like */
13
#define MAX 1000
14
15
/* define our functions*/
16
int getNewLine(char line[], int max);
17
void copy(char to[], char from[]);
18
19
int main(){
20
21
int len, max;
22
char line[MAX];
23
char longest[MAX];
24
25
max = 0;
26
while((len = getNewLine(line, MAX)) > 0){
27
28
if(len > max){
29
30
max = len;
31
copy(longest, line);
32
33
}
34
35
}
36
37
if (max > 0){
38
39
printf("length = %i, string=%s", max, longest);
40
41
}
42
43
return 0;
44
45
}
46
47
/* get a line in a character array */
48
int getNewLine(char arr[], int lim){
49
50
int c, i;
51
52
for(i=0; i < lim -1 && (c=getchar()) != EOF && c != '\n'; ++i){
53
54
arr[i] = c;
55
56
}
57
if(c == '\n'){
58
59
arr[i] = c;
60
++i;
61
62
}
63
64
else{
65
66
/* Continue to count the length even if it is longer than the max */
67
while((c=getchar()!=EOF) && c != '\n'){
68
69
++i;
70
71
}
72
73
if(c == '\n'){
74
75
arr[i] = c;
76
++i;
77
78
}
79
80
}
81
82
arr[i] = '\0';
83
return i;
84
85
}
86
87
/* copy one character array to another */
88
void copy(char to[], char from[]){
89
90
int i;
91
92
i = 0;
93
94
while((to[i] = from[i]) != '\0'){
95
96
++i;
97
98
}
99
100
}
101
102