Hello all! First post here! I look forward to receiving your generous help so eventually I will be decent enough to help others. Anyways..straight to the point...
So I am learning to program in C using the K&R book. I am struggling with a problem, in the book. For those with the book, chapter 1.9, exercise 1-16.
For those without the book; The book has given me a piece of code that has the main function and two other functions. The original code simply took in an input of lines, and printed out the longest line as allowed by the MAXLINE number. The exercise now wants me to modify only the MAIN code so that it prints out the actual length of the longest line, and as much as possible of the line as limited by MAXLINE.
The way I attempted to do it does not even pass the most simple test, in which I only give it one line. It skips the first character and prints out the rest that is possible according to the limit, but it also returns back a length of 0, which is not valid. Can somebody help me please?
This is the original code from the book...
Code:#include <stdio.h> #define MAXLINE 1000 int getLine(char line[], int maxline); void copy(char to[], char from[]); //prints longest input line main(){ int len; //current line length int max; //maximum length seen so far char line[MAXLINE]; //current input line char longest[MAXLINE]; //longest line saved here max = 0; while ((len = getline(line, MAXLINE)) > 0) if (len > max){ max = len; copy(longest, line); } if (max > 0){ //there was a line putchar('\n'); printf("%s", longest); } return 0; } //read a line into s, return length int getline(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 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; }
Here is my modified version...
Thank you for your future help!Code:#include <stdio.h> #define MAXLINE 10 int getline(char line[], int maxline); void copy(char to[], char from[]); //prints longest input line main(){ int c; //the char int len; //current line length int max; //maximum length seen so far int maxlen; //longest line int possiblemax; //possibly max line char line[MAXLINE]; //current input line char longest[MAXLINE]; //longest line saved here //sets default values max = maxlen = possiblemax = 0; //while loop for checking end of input while ((c = getchar()) != EOF){ //if case for when the char is equal to a new line if (c == '\n'){ //when char is equal to a new line //and possiblemax is greater than maxlen //change maxlen to equal to possible max //reset possible max to 0 if (possiblemax > maxlen){ maxlen = possiblemax; possiblemax = 0; } //else just set possiblemax back to 0 else possiblemax = 0; } //if not a new line, add 1 to possiblemax else ++possiblemax; while ((len = getline(line, MAXLINE)) > 0) if (len > max){ max = len; copy(longest, line); } if (max > 0){ //there was a line //prints maxlen value printf("length:%d", maxlen); putchar('\n'); printf("%s", longest); } return 0; } } //read a line into s, return length int getline(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 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; }



LinkBack URL
About LinkBacks


