aboutsummaryrefslogtreecommitdiff
path: root/ch1/1-16_longest-line.c
diff options
context:
space:
mode:
authorzlg <zlg@zlg.space>2015-12-19 18:40:02 -0800
committerzlg <zlg@zlg.space>2015-12-19 18:40:02 -0800
commit0c7a59180de778a4e9019176a65eb02b5493b6f7 (patch)
tree967bf532cf9cc9ed2d0416257b8102dcd29aaded /ch1/1-16_longest-line.c
parentChange license to GPLv3, add contributing guide (diff)
downloadknr-0c7a59180de778a4e9019176a65eb02b5493b6f7.tar.gz
knr-0c7a59180de778a4e9019176a65eb02b5493b6f7.tar.bz2
knr-0c7a59180de778a4e9019176a65eb02b5493b6f7.tar.xz
knr-0c7a59180de778a4e9019176a65eb02b5493b6f7.zip
1-16 solution code and comment style cleanup
Also of note is the length that the program reports includes the newline character at the end. This has been clarified in a comment. The exercise text does not specify whether to include newlines as part of the length, so it was left alone. Removed the contributor notice, since contributor acknowledgements are centralized.
Diffstat (limited to 'ch1/1-16_longest-line.c')
-rw-r--r--ch1/1-16_longest-line.c92
1 files changed, 44 insertions, 48 deletions
diff --git a/ch1/1-16_longest-line.c b/ch1/1-16_longest-line.c
index 450d990..3c86570 100644
--- a/ch1/1-16_longest-line.c
+++ b/ch1/1-16_longest-line.c
@@ -10,70 +10,66 @@
* to tackle a problem in chunks of memory instead of all at once. It's
* slightly more complicated, but adds usefulness to a program.
*
- * This solution, which was contributed by Isa (ibnIrshad), follows the spec
- * exactly, by only modifying main(), unlike many other solutions on the
- * internet.
+ * This solution follows the spec exactly, by only modifying main(), unlike many
+ * other solutions on the internet.
*
* Assumptions:
* 1. "arbitrarily long" is interpreted to mean upto the maximum size of an
* integer on the given architechture, e.g. 2^32 unsigned on a 32-bit machine.
- * Indeed a string of that size would be larger than 4 gigabytes.
- * It is possible to deal with numbers larger than that, but it involves a great
- * deal of work abstracting away the concept of an integer, similar to
- * dynamically sized arrays.
+ * Indeed a string of that size would be larger than 4 gigabytes. It is possible
+ * to deal with numbers larger than that, but it involves a great deal of work
+ * abstracting away the concept of an integer, similar to dynamically sized
+ * arrays.
*
* 2. EOF signal (Ctrl-D) must be given on an empty line
*/
-#define BUFFSIZE 5 /* renamed MAXLINE and reduced it to 5 to demonstrate
-we can handle lines much greater than this number*/
+
+/* demonstrate that we can handle lines much greater than this number*/
+#define BUFFSIZE 5
int getlinelen(char line[], int maxline);
void copy(char to[], char from[]);
/* print longest input line */
-main()
-{
- /* len of current line, max len seen so far, templen of buffer */
- int len, max, templen;
- char buffer[BUFFSIZE];
-
- max = len = 0;
-
- while ((templen = getlinelen(buffer, BUFFSIZE)) > 0) {
- len += templen;
-
- if (buffer[templen-1] == '\n'){
- if (len > max)
- max = len;
- len = 0;
- }
- }
- printf("\nLen of longest line: %d\n", max);
- return 0;
+main() {
+ /* len of current line, max len seen so far, templen of buffer */
+ int len, max, templen;
+ char buffer[BUFFSIZE];
+ max = len = 0;
+ while ((templen = getlinelen(buffer, BUFFSIZE)) > 0) {
+ len += templen;
+ if (buffer[templen - 1] == '\n') {
+ if (len > max) {
+ max = len;
+ }
+ len = 0;
+ }
+ }
+ /* The length returned includes the '\n' character. */
+ printf("\nLen of longest line: %d\n", max);
+ return 0;
}
/* getlinelen: read a line into s, return length */
-int getlinelen(char s[], int lim)
-{
- int c, i;
-
- for (i=0; i<lim-1 && (c=getchar()) != EOF && c!='\n'; ++i)
- s[i] = c;
- if (c == '\n') {
- s[i] = c;
- ++i;
- }
- s[i] = '\0';
- return i;
+int getlinelen(char s[], int lim) {
+ int c, i;
+ for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) {
+ s[i] = c;
+ }
+ if (c == '\n') {
+ s[i] = c;
+ ++i;
+ }
+ s[i] = '\0';
+ return i;
}
-/* copy: copy 'from into 'to'; assume to is big enough */
-void copy(char to[], char from[])
-{
- int i;
-
- i = 0;
- while ((to[i] = from[i]) != '\0')
- ++i;
+/* copy: copy 'from' into 'to'; assume 'to' is big enough */
+void copy(char to[], char from[]) {
+ int i;
+ i = 0;
+ while ((to[i] = from[i]) != '\0') {
+ ++i;
+ }
}