Skeleton of exercise 1-16; functionally a verbatim copy of the example text on page 30 of KNR C, though in reality, I have made it more readable and fixed a couple of bugs related to expanded functionality of modern C.
This commit is contained in:
parent
4ddc868da3
commit
c47e378a4f
1 changed files with 71 additions and 0 deletions
71
KNR C/ex1_16.c
Normal file
71
KNR C/ex1_16.c
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
#include <stdio.h>
|
||||
|
||||
/*A program for finding the longest line in its input, and then returning the
|
||||
* longest. Primarily copied from page 30 of KNR C by Kernigham Ritchie.
|
||||
* Annotation is extremely verbose so that I could break this code down for
|
||||
* myself in a way that made more sense than as-written.*/
|
||||
|
||||
#define MAXLINE 1000 //Define maximum allowable line length.
|
||||
|
||||
//Forward declaration for a function that will retreive the current line and
|
||||
//store it in array line[]. Since modern C actually has a getline function, we
|
||||
//declare this as pullline
|
||||
int pullLine(char line[], int maxline);
|
||||
//Forward declaration for a function to copy between arrays.
|
||||
void copy(char from[], char to[]);
|
||||
|
||||
int main(){
|
||||
|
||||
//Declare ints related to tracking line lengths.
|
||||
int currentLength; //Length of current line; originally len.
|
||||
int max = 0; //Length of longest line encountered; originally max.
|
||||
//Initialized as 0 as opposed to using a separate
|
||||
//statement to set value as in original code.
|
||||
|
||||
char line[MAXLINE]; //Initialize array for storing current line.
|
||||
char longest [MAXLINE]; //Initialize array for storing longest line so far.
|
||||
|
||||
while ((currentLength = pullLine(line, MAXLINE)) > 0){
|
||||
if (currentLength > max){
|
||||
max = currentLength;
|
||||
copy(line, longest);
|
||||
}
|
||||
}
|
||||
if (max > 0) //If any lines were encountered, print the longest.
|
||||
printf("%s", longest);
|
||||
return 0;
|
||||
}
|
||||
|
||||
//pullLine will read a line into array s[] and return its length.
|
||||
int pullLine(char s[], int lim){
|
||||
int c, i;
|
||||
/*Set i= 0, check if i is < lim, which is MAXLINE as written AND that
|
||||
* getchar returns a non EOF character (while stepping c) AND that the
|
||||
* character pulled (c), is not a newline character, then increment i. The
|
||||
* actual body of this loop simply stores the value of the retrieved
|
||||
* character in array s[] at position |i|.*/
|
||||
for (i = 0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
|
||||
s[i] = c;
|
||||
|
||||
if (c == '\n'){ //If |c| is \n, then still set pos \i\ in array s[] and
|
||||
//increment i. This has to be handled outside the for loop,
|
||||
//since c =='\n' is a break condition.
|
||||
s[i] = c;
|
||||
++i;
|
||||
}
|
||||
s[i] = '\0'; //Set a null value at the end of the line for termination.
|
||||
return i; //Return the length of the line pulled.
|
||||
}
|
||||
|
||||
//Function for copying one array to another. I chose to write this as
|
||||
//from -- > to instead of to --> from because it's not the 90s any more and
|
||||
//I feel like I'm allowed to have a little temporal linearity as a treat.
|
||||
void copy(char from[], char to[]){
|
||||
int i = 0; //I have once again initialized and declared in the same line
|
||||
//because it is 2025.
|
||||
|
||||
//Set the value of pos \i\ in to[] to the value of pos \i\ in from as long
|
||||
//as it is not null.
|
||||
while ((to[i] = from[i]) != '\0')
|
||||
++i;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue