-
Notifications
You must be signed in to change notification settings - Fork 0
/
function_char_longest-line.c
137 lines (108 loc) · 3.8 KB
/
function_char_longest-line.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* @file function_char_longest-line.c
* @author kunal jain ([email protected])
* @brief Program to read input line by line and
* return longest line from the input.
* This program uses functions, character I/O,
* and arrays that we learnt in past classes. This
* single program sums it up all.
* Assume maximum length of any line in input is 1000 characters.
* @version 0.1
* @date 2022-02-05
*
* @copyright Copyright (c) 2022
*
*/
// while (there is another line)
// if (it's longer than the previous longest)
// save it
// save its length
// print longest line
#include<stdio.h>
// Function to read input till new line character and
// get length of line.
// Paramters - char array to store characters that are being read.
// Return - length of a line as integer
int getlinelength(char input[]) {
// Read a character till new line or EOF.
// If first character is new line then length of line = 1
// Put null ('\0') character at the end of the array
// to mark end of string of character.
// Because length of line = number of characters read,
// return i
int character, i;
// There are two test conditions with AND operator.
// Loop will stop if any one of the condition is false.
//
// Any changes in char input[] means changes in char line[],
// because array passed is pointer to its beginning location.
for (i = 0; (character = getchar()) != EOF && character != '\n'; ++i) {
input[i] = character;
}
// Added new line character in the end of line as
// line with only new line character has length of one.
if (character == '\n') {
input[i] = character;
++i;
}
// Put character '\0' (null character) at the end of the array
// to mark the end of the string of characters.
input[i] = '\0';
// Return length of line which is also equal to number
// of characters read
return i;
}
// Function to copy characters from one char array to another
// This copies 'from' into 'to'.
// Parameter - char from[], char to[]
// Return - void
void copy(char from[], char to[]) {
int i = 0;
// Copy 'from' into 'to' one character at a time
// until null character encountered.
while ((to[i] = from[i]) != '\0') {
i++;
}
// Any changes in 'to' array means changes in longestline array.
// This function does not return anything.
// Such functions have "void" return type.
}
int main(void) {
// Variable declarations
// Variable len to hold length of each line.
// Variable max to hold length of max line length.
int len, max = 0;
// Character array to store line from input.
char line[1000];
// Character array to store longest line.
// We will copy char array line into char array
// longestline if lenght of line is greater
// than current max length.
char longestline[1000];
// Get length of each line using getlinelength function
while ((len = getlinelength(line)) > 0) {
// If length of line greater than current max length
// then update max variable and copy line into
// longestline.
if (len > max) {
max = len;
copy(line, longestline);
}
}
// If max length of any line read is greater than zero
// then print longestline.
if (max > 0) {
printf("Longest line in the input is -> ");
// Two ways to print string in C.
// First way - using loop
// int i;
// for (i = 0; i < max; i++) {
// printf("%c", longestline[i]);
// }
// Second way
// %s -> Format specifier to print sequence of characters
printf("%s", longestline);
printf("\n");
}
return 0;
}