aboutsummaryrefslogtreecommitdiff
path: root/ch1
diff options
context:
space:
mode:
Diffstat (limited to 'ch1')
-rw-r--r--ch1/1-16_longest-line.c104
1 files changed, 60 insertions, 44 deletions
diff --git a/ch1/1-16_longest-line.c b/ch1/1-16_longest-line.c
index 137bb11..450d990 100644
--- a/ch1/1-16_longest-line.c
+++ b/ch1/1-16_longest-line.c
@@ -9,55 +9,71 @@
* Answer: The key to arbitrary limits is buffering. Using a buffer allows you
* 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.
+ *
+ * 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.
+ *
+ * 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*/
+
+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];
-#define MAXLENGTH 100
-
-int get_line(char s[], int lim) {
- /* Put as much as possible into a temp string, and count its length */
- int c, i;
-
- for (i = 0; i < lim && (c = getchar()) != EOF && c != '\n'; ++i) {
- s[i] = c;
- }
- if (c == '\n') {
- s[i] = c;
- ++i;
- }
- s[i] = '\0';
- return i;
+ 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;
}
-void copy(char from[], char to[]) {
- int i = 0;
+/* getlinelen: read a line into s, return length */
+int getlinelen(char s[], int lim)
+{
+ int c, i;
- while ((to[i] = from[i]) != '\0') {
- ++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 main() {
- int len, max;
-
- char line[MAXLENGTH];
- char longest[MAXLENGTH];
-
- max = 0;
- while ((len = get_line(line, MAXLENGTH)) > 0) {
- if (len > max) {
- max = len;
- copy(line, longest);
- }
- }
-
- printf("\nThe longest line is %3d characters long.\n", max);
- printf("----------------------------------------\n");
- if (max > 0) {
- printf("%-s", longest);
- if (max == MAXLENGTH && longest[max - 1] != '\n') {
- printf("\n");
- }
- }
-
- return 0;
+
+/* 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;
}
0700'>2018-09-04Update LICENSE to match setup.pyzlg1-80/+67 Whoops. 2018-09-03Branch off from master with pytest, tox, clickzlg16-778/+779 This commit is huge, but contains everything needed for a "proper" build system built on pytest + tox and a CLI built with click. For now, this branch will contain all new vgstash development activity until it reaches feature parity with master. The CLI is installed to pip's PATH. Only the 'init', 'add', and 'list' commands work, with only two filters. This is pre-alpha software, and is therefore not stable yet. 2018-03-18Flesh out filter types and ownership statuszlg3-82/+144 It's time for a refactor to a module; the functionality and interface are clashing. 2018-03-18README.mdown: break line correctlyzlg1-1/+1 2018-03-18add 'playlog' list filterzlg2-2/+9 This filter is used to get an idea of which games you're currently playing through, so you can prioritize games to play when you're bored and detect it when you've beaten a game but haven't marked it as such. 2018-03-13Update helpers a bitzlg1-2/+9 At present, user modification is needed to make these seamless. vgup() may need to be axed in favor of telling the user to make an alias. 2018-03-13Make VGSTASH_DB_LOCATION point to a filezlg2-21/+20 It used to point to a directory, which would then look for .vgstash.db. This behavior was kind of backwards and I don't remember why I did it that way. This change gives users more control over where they put their DB. Be sure to update your environment variable if you have it set! 2016-11-18Remove settings from helpers.shZe Libertine Gamer1-5/+0 Sourcing them in .bash_profile screws up login if they're set. 2016-11-15Correct phrasing in README.Ze Libertine Gamer1-4/+4 2016-11-13DerpZe Libertine Gamer1-0/+1 2016-11-03Improve error handling in shell scriptsZe Libertine Gamer4-3/+23 2016-10-24Correct run_again, add recursionZe Libertine Gamer1-0/+4 Loops and functions -- oh my, what a useful combination. :) 2016-10-21Add quotes to correct behavior for arglistZe Libertine Gamer1-1/+1 2016-10-14updater.sh: add recursion, error handlingZe Libertine Gamer1-43/+101 2016-10-14Correct pipe-handling behaviorZe Libertine Gamer1-1/+9 2016-10-12Clarify a method to move between platformsZe Libertine Gamer1-2/+5 Also correct a typo.